Preloader

Basic SQL Queries

PHP Tutorials
codevigyaan php

Basic SQL queries are the starting point for working with databases. They help you create databases, create tables, and manage data stored inside them. Every SQL learner must understand these commands before moving to advanced topics.

In this lesson, you will learn the most common SQL queries used in daily database work.

CREATE DATABASE

The CREATE DATABASE command is used to create a new database.

Syntax

CREATE DATABASE database_name;

Example

CREATE DATABASE school;

This command creates a database named school.

USE DATABASE

The USE command selects a database so you can work inside it.

Syntax

USE database_name;

Example

USE school;

After this, all tables and queries will apply to the school database.

CREATE TABLE

The CREATE TABLE command is used to create a new table inside a database.

Syntax

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype
);

Example

CREATE TABLE students (
    id INT,
    name VARCHAR(100),
    email VARCHAR(100)
);

This creates a table named students with three columns.

ALTER TABLE

The ALTER TABLE command is used to change an existing table structure.

Add a new column

ALTER TABLE students
ADD age INT;

Modify a column

ALTER TABLE students
MODIFY name VARCHAR(150);

Delete a column

ALTER TABLE students
DROP email;

ALTER is very useful when your table structure needs updates.

RENAME TABLE

The RENAME command is used to change the table name.

Example

RENAME TABLE students TO student_details;

Now the table name becomes student_details.

(Some databases also support)

ALTER TABLE students
RENAME TO student_details;

INSERT Query

The INSERT query adds new records to a table.

Example

INSERT INTO student_details (id, name, age)
VALUES (1, 'Rahul', 20);

SELECT Query

The SELECT query retrieves data from a table.

Example

SELECT * FROM student_details;

To fetch specific columns

SELECT name, age FROM student_details;

WHERE Clause

The WHERE clause filters records based on conditions.

Example

SELECT * FROM student_details
WHERE age = 20;

UPDATE Query

The UPDATE query modifies existing records.

Example

UPDATE student_details
SET age = 21
WHERE id = 1;

Always use WHERE to avoid updating all rows.

DELETE Query

The DELETE query removes records from a table.

Example

DELETE FROM student_details
WHERE id = 1;

ORDER BY Clause

The ORDER BY clause sorts the result.

Example

SELECT * FROM student_details
ORDER BY name ASC;

LIMIT Clause

The LIMIT clause controls the number of rows returned.

Example

SELECT * FROM student_details
LIMIT 5;

Why These SQL Queries Are Important

These basic SQL commands help you
create databases and tables
store and manage data
update table structure
build real world applications

They form the base for learning joins, functions, and advanced SQL.

Summary

CREATE DATABASE creates a database.
USE selects the database.
CREATE TABLE builds tables.
ALTER and RENAME change table structure.
INSERT, SELECT, UPDATE, and DELETE manage data.

Once these basics are clear, learning advanced SQL becomes much easier.

Check out our resources!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *