Top 10 SQL Commands with Syntax and Examples
1. CREATE TABLE
Purpose: Create a new table.Description: Defines table structure with columns and datatypes.
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype
);
Example:
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
marks INT
);
2. INSERT
Purpose: Add new data into table.Description: Inserts new records.
Syntax:
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
Example:
INSERT INTO students (name, marks)
VALUES ('Arun', 85);
3. SELECT
Purpose: Retrieve data from table.Description: Displays records from database.
Syntax:
SELECT column1, column2 FROM table_name;
Example:
SELECT * FROM students;
4. UPDATE
Purpose: Modify existing data.Description: Changes records in table.
Syntax:
UPDATE table_name
SET column1=value1
WHERE condition;
Example:
UPDATE students
SET marks=90
WHERE id=1;
5. DELETE
Purpose: Remove data from table.Description: Deletes specific records.
Syntax:
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM students
WHERE id=2;
6. DROP TABLE
Purpose: Delete entire table.Description: Removes table permanently.
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE students;
7. ALTER TABLE
Purpose: Modify table structure.Description: Add, delete, or modify columns.
Syntax:
ALTER TABLE table_name
ADD column datatype;
Example:
ALTER TABLE students
ADD email VARCHAR(100);
8. WHERE
Purpose: Filter records.Description: Select records that meet condition.
Syntax:
SELECT * FROM table_name
WHERE condition;
Example:
SELECT * FROM students
WHERE marks > 80;
9. ORDER BY
Purpose: Sort records.Description: Arrange data ascending or descending.
Syntax:
SELECT * FROM table_name
ORDER BY column ASC|DESC;
Example:
SELECT * FROM students
ORDER BY marks DESC;
10. JOIN
Purpose: Combine data from multiple tables.Description: Retrieves related data from two tables.
Syntax:
SELECT columns
FROM table1
JOIN table2
ON table1.column = table2.column;
Example:
SELECT students.name, results.grade
FROM students
JOIN results
ON students.id = results.student_id;
Comments
Post a Comment