DEV Community

Deepangshi S.
Deepangshi S.

Posted on • Edited on

2 1 1 1 1

Day 3: SQL Command Types – DDL, DML, DQL, DCL, TCL : Mastering

SQL Command Types

SQL commands are categorized into five key types based on their functionality:

  • DDL (Data Definition Language): Manage database structures.
  • DML (Data Manipulation Language): Modify and interact with data.
  • DQL (Data Query Language): Retrieve data from the database.
  • DCL (Data Control Language): Control access to the database.
  • TCL (Transaction Control Language): Manage database transaction.

i.) Data Definition Language (DDL)
Used to define, alter, and remove database structures like tables, schemas, and indexes.

Key Commands:

  • CREATE: Create new databases, tables, or other objects.
CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    grade INT
);
Enter fullscreen mode Exit fullscreen mode
  • ALTER: Modify existing database objects.
ALTER TABLE students ADD COLUMN email VARCHAR(100);
Enter fullscreen mode Exit fullscreen mode
  • DROP: Remove database objects permanently.
DROP TABLE students;
Enter fullscreen mode Exit fullscreen mode
  • TRUNCATE: Remove all data from a table but keep its structure.
TRUNCATE TABLE students;
Enter fullscreen mode Exit fullscreen mode

ii.) Data Manipulation Language (DML)
Focuses on inserting, updating, and deleting data in tables.

Key Commands:

  • INSERT: Add new data.
INSERT INTO employees (emp_id, name, department) VALUES (1, 'Alice', 'HR');
Enter fullscreen mode Exit fullscreen mode
  • UPDATE: Modify existing data.
UPDATE employees SET department = 'Finance' WHERE emp_id = 1;
Enter fullscreen mode Exit fullscreen mode
  • DELETE: Remove specific records.
DELETE FROM employees WHERE emp_id = 1;
Enter fullscreen mode Exit fullscreen mode

iii.) Data Query Language (DQL)
Focuses on retrieving data using the SELECT statement.
Key Command:

  • SELECT: Fetch data from the database.
SELECT * FROM employees WHERE department = 'HR';
Enter fullscreen mode Exit fullscreen mode

iv.) Data Control Language (DCL)
Used to control access and permissions for the database.
Key Commands:

  • GRANT: Give privileges to users.
GRANT SELECT, INSERT ON employees TO user1;
Enter fullscreen mode Exit fullscreen mode
  • REVOKE: Remove privileges from users.
REVOKE INSERT ON employees FROM user1;
Enter fullscreen mode Exit fullscreen mode

v.) Transaction Control Language (TCL)
Used to manage database transactions, ensuring data consistency.
Key Commands:

  • COMMIT: Save changes in a transaction.
COMMIT;
Enter fullscreen mode Exit fullscreen mode
  • ROLLBACK: Undo changes made in a transaction.
ROLLBACK;
Enter fullscreen mode Exit fullscreen mode
  • SAVEPOINT: Set a save point to rollback to.
SAVEPOINT: Set a save point to rollback to.
Enter fullscreen mode Exit fullscreen mode
  • SET TRANSACTION: Define transaction properties.
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
Enter fullscreen mode Exit fullscreen mode

Challenge for you: High-Earning Employees with Department Insights
📌 Task:

Write a single SQL query to:

Retrieve employee names, their department names, and their salaries.
Calculate the department-wise average salary.
Only show employees earning more than the average salary of their department.
Sort the results in descending order of salary.
💡 Hint: Use JOIN, GROUP BY, HAVING, ORDER BY, and a subquery.

Neon image

Serverless Postgres in 300ms (❗️)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (0)

Image of Quadratic

Python + AI + Spreadsheet

Chat with your data and get insights in seconds with the all-in-one spreadsheet that connects to your data, supports code natively, and has built-in AI.

Try Quadratic free

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay