SQL Queries: DDL, DML, DCL, TCL (Examples)
1. Create a table Students with columns (student_id, name, dept, join_year).
Query: CREATE TABLE Students (student_id INT PRIMARY KEY, name VARCHAR
(50), dept VARCHAR (20), join_year INT);
2. Insert a student (101, 'Aditi', 'CSE', 2023).
Query: INSERT INTO Students VALUES (101, 'Aditi', 'CSE', 2023);
3. Update department of student 101 to 'ECE'.
Query: UPDATE Students SET dept='ECE' WHERE student_id=101;
4. Delete student with id 101.
Query: DELETE FROM Students WHERE student_id=101;
5. Add a new column email to Students table.
Query: ALTER TABLE Students ADD COLUMN email VARCHAR (100);
6. Rename column dept to department.
Query: ALTER TABLE Students RENAME COLUMN dept TO department;
7. Create a user 'analyst' with password 'pwd123'.
Query: CREATE USER analyst IDENTIFIED BY 'pwd123';
8. Grant SELECT privilege on Students to analyst.
Query: GRANT SELECT ON Students TO analyst;
9. Revoke INSERT privilege from analyst on Students.
Query: REVOKE INSERT ON Students FROM analyst;
10. Start a transaction, insert a row into Students, then rollback.
Query: START TRANSACTION; INSERT INTO Students VALUES (102, 'Rahul',
'ME', 2022); ROLLBACK;
11. Start a transaction, insert a row into Students, then commit.
Query: START TRANSACTION; INSERT INTO Students VALUES (103, 'Neha',
'CSE', 2024); COMMIT;
12. Drop the column email from Students table.
Query: ALTER TABLE Students DROP COLUMN email;
13. Create a table Courses with (course_id, title, credits).
Query: CREATE TABLE Courses (course_id INT PRIMARY KEY, title VARCHAR
(50), credits INT);
14. Create a table Faculty with (faculty_id, name, designation, dept)
Query: CREATE TABLE Faculty (faculty_id INT PRIMARY KEY, name VARCHAR
(50), designation VARCHAR (30), dept VARCHAR (20));
15. Add NOT NULL constraint on [Link] column.
Query: ALTER TABLE Students ALTER COLUMN name SET NOT NULL;
16. Drop the table Courses.
Query: DROP TABLE Courses;
17. Truncate Students table (remove all rows but keep structure).
Query: TRUNCATE TABLE Students;
18. Change name of student 103 to 'Neha Sharma'.
Query: UPDATE Students SET name='Neha Sharma' WHERE student_id=103;