Important SQL Commands

 

 

1. CREATE DATABASE

Purpose

Used to create a new database.

Syntax

CREATE DATABASE database_name;

Example

CREATE DATABASE SchoolDB;

Exam Question Example

Write a SQL command to create a database named LIBRARY.

CREATE DATABASE LIBRARY;

2. CREATE TABLE

Purpose

Used to create a new table inside a database.

Syntax

CREATE TABLE table_name(
column_name datatype,
column_name datatype
);

Example

CREATE TABLE student(
rollno INT,
name VARCHAR(20),
class VARCHAR(5),
marks INT
);

3. DESC (DESCRIBE)

Purpose

Displays the structure of a table.

Syntax

DESC table_name;

Example

DESC student;

Output shows

  • column names

  • data types

  • null information

4. INSERT INTO

Purpose

Used to add records into a table.

Syntax

INSERT INTO table_name
VALUES(value1,value2,value3);

Example

INSERT INTO student
VALUES(101,‘Rahul’,‘XI’,85);

Insert using column names

INSERT INTO student(rollno,name,marks)
VALUES(102,‘Riya’,90);

5. SELECT

Purpose

Used to retrieve data from a table.

Syntax

SELECT column_name
FROM table_name;

Example

SELECT name FROM student;

6. SELECT *

Purpose

Display all columns from the table.

Syntax

SELECT * FROM table_name;

Example

SELECT * FROM student;

7. DISTINCT

Purpose

Used to remove duplicate values.

Syntax

SELECT DISTINCT column_name
FROM table_name;

Example

SELECT DISTINCT city
FROM employee;

8. WHERE

Purpose

Used to apply conditions while retrieving data.

Syntax

SELECT * FROM table_name
WHERE condition;

Example

SELECT * FROM student
WHERE marks > 80;

9. UPDATE

Purpose

Used to modify existing records.

Syntax

UPDATE table_name
SET column=value
WHERE condition;

Example

UPDATE student
SET marks = 88
WHERE rollno = 101;

10. DELETE

Purpose

Used to remove records from a table.

Syntax

DELETE FROM table_name
WHERE condition;

Example

DELETE FROM student
WHERE rollno = 103;

NOTE :  If WHERE clause is omitted, all records will be deleted.

 

11. ALTER TABLE

Purpose

Used to modify the structure of a table.

Add Column

ALTER TABLE student
ADD address VARCHAR(30);

Modify Column

ALTER TABLE student
MODIFY name VARCHAR(25);

Drop Column

ALTER TABLE student
DROP address;

12. DROP TABLE

Purpose

Used to delete a table permanently.

Syntax

DROP TABLE table_name;

Example

DROP TABLE student;

13. ORDER BY

Purpose

Used to sort records.

Syntax

SELECT * FROM table_name
ORDER BY column_name;

Ascending Order

SELECT * FROM student
ORDER BY marks ASC;

Descending Order

SELECT * FROM student
ORDER BY marks DESC;

14. LIKE

Purpose

Used for pattern matching.

Symbols Used

Symbol Meaning
% Any number of characters
_ Single character

Example

Names starting with R

SELECT * FROM student
WHERE name LIKE ‘R%’;

Names ending with a

SELECT * FROM student
WHERE name LIKE ‘%a’;

15. BETWEEN

Purpose

Used to select values within a range.

Syntax

SELECT * FROM table_name
WHERE column BETWEEN value1 AND value2;

Example

SELECT * FROM student
WHERE marks BETWEEN 70 AND 90;

16. IN

Purpose

Used to match values from a list.

Syntax

SELECT * FROM table_name
WHERE column IN (value1,value2);

Example

SELECT * FROM student
WHERE class IN (‘XI’,‘XII’);

17. Logical Operators

Used to combine conditions.

Operator Purpose
AND Both conditions must be true
OR At least one condition true
NOT Reverse condition

Example

SELECT * FROM student
WHERE marks > 80 AND class=‘XI’;

Data Manipulation (DML)

Used to add, change, or remove the actual records (rows).

  • INSERT: INSERT INTO student VALUES (101, 'Rahul', 'XI', 85);

  • UPDATE: UPDATE student SET marks = 88 WHERE rollno = 101;

    • ⚠️ Note: Always use WHERE or all rows will be updated!

  • DELETE: DELETE FROM student WHERE rollno = 103;

    • ⚠️ Note: DELETE FROM student; removes all data but keeps the table structure.

Data Retrieval & Filtering (DQL)

The most common commands for fetching specific information.

The Basic Select

  • SELECT * FROM student; (Show all columns)

  • SELECT name, marks FROM student; (Show specific columns)

  • SELECT DISTINCT city FROM employee; (Removes duplicate entries)

Filtering with WHERE

  • Relational: WHERE marks > 80;

  • List Match: WHERE class IN ('XI', 'XII');

  • Range: WHERE marks BETWEEN 70 AND 90;

  • Pattern Matching (LIKE):

    • LIKE 'R%' → Starts with ‘R’

    • LIKE '%a' → Ends with ‘a’

    • LIKE '_i%' → ‘i’ is the second character

Sorting & Logic

  • ORDER BY: ORDER BY marks DESC; (Use ASC for smallest to largest)

  • AND / OR: Combine conditions (e.g., WHERE marks > 80 AND class = 'XI')

Exam Tips for Students

  1. Semicolons: Don’t forget to end every command with ;.

  2. Quotes: Use single quotes ' ' for VARCHAR, CHAR, and DATE values. Numbers do not need quotes.

  3. DROP vs DELETE: DROP kills the table; DELETE kills the rows inside the table.

  4. NULL: Use IS NULL or IS NOT NULL to check for empty values (never use = NULL).

Recommended Posts