SQL statement categories (DDL, DML, DQL, DCL) - Time & Space Complexity
We want to understand how the time to run different types of SQL statements changes as the data or structure grows.
Which parts of SQL take more or less time when the database gets bigger?
Analyze the time complexity of these example SQL statements from different categories.
-- DDL: Create a table
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(100)
);
-- DML: Insert data
INSERT INTO Employees (ID, Name) VALUES (1, 'Alice');
-- DQL: Select data
SELECT * FROM Employees WHERE ID = 1;
-- DCL: Grant permission
GRANT SELECT ON Employees TO User1;
These statements show different SQL categories: DDL, DML, DQL, and DCL.
Look at what repeats or loops happen inside each statement type.
- DDL: Usually runs once to change structure, no loops over data.
- DML: Inserts or updates data, may loop over rows if batch inserting.
- DQL: Searches data rows, loops over matching rows to return results.
- DCL: Changes permissions, runs once, no data loops.
How does time grow when data or structure size grows?
| Input Size (n) | Approx. Operations |
|---|---|
| 10 rows | DDL: constant, DML: 10 inserts, DQL: search 10 rows, DCL: constant |
| 100 rows | DDL: constant, DML: 100 inserts, DQL: search 100 rows, DCL: constant |
| 1000 rows | DDL: constant, DML: 1000 inserts, DQL: search 1000 rows, DCL: constant |
DDL and DCL time stays about the same no matter data size. DML and DQL time grows roughly with the number of rows affected or searched.
Time Complexity: O(n) for DML; O(1) for DDL and DCL; O(log n) for DQL with index, otherwise O(n)
This means data-changing and data-reading statements take longer as data grows, but structure or permission changes take about the same time.
[X] Wrong: "All SQL statements take the same time no matter data size."
[OK] Correct: Some statements work on data rows and take longer as data grows, while others change structure or permissions and take about the same time.
Knowing how different SQL statements scale helps you write efficient queries and understand database behavior in real projects.
"What if a SELECT query uses an index? How would that affect its time complexity?"
Practice
Solution
Step 1: Understand the role of DDL
DDL commands define or change database structures, such as creating tables or indexes.Step 2: Differentiate from other categories
DML changes data, DQL queries data, and DCL controls access, so they don't modify structure.Final Answer:
Data Definition Language (DDL) -> Option CQuick Check:
Structure changes = DDL [OK]
- Confusing DDL with DML because both change something
- Thinking SELECT is DDL instead of DQL
- Mixing up DCL with DDL
employees to a user john?Solution
Step 1: Recall correct GRANT syntax
The correct syntax is: GRANT permission ON object TO user;Step 2: Match syntax with options
GRANT SELECT ON employees TO john; matches the correct order and keywords exactly.Final Answer:
GRANT SELECT ON employees TO john; -> Option DQuick Check:
GRANT + permission + ON + object + TO + user [OK]
- Swapping TO and ON keywords
- Placing user before permission
- Using SELECT before GRANT
SELECT * FROM employees WHERE salary > 50000;
Solution
Step 1: Understand the SELECT query
The query selects all columns (*) from employees where salary is greater than 50000.Step 2: Analyze the WHERE condition
Only rows with salary > 50000 are returned; others are excluded.Final Answer:
All rows from employees with salary greater than 50000 -> Option AQuick Check:
WHERE salary > 50000 filters rows correctly [OK]
- Confusing > with <
- Thinking semicolon is mandatory for output
- Assuming salary column missing without info
INSERT INTO employees (id, name salary) VALUES (1, 'Alice', 60000);
Solution
Step 1: Check column list syntax
Columns must be separated by commas; here 'name' and 'salary' lack a comma.Step 2: Verify other parts
VALUES is correct, table name does not need quotes, numeric salary is valid.Final Answer:
Missing comma between columns 'name' and 'salary' -> Option AQuick Check:
Column list commas are required [OK]
- Ignoring missing commas in column list
- Thinking table names need quotes
- Assuming VALUES keyword typo
orders and all its data permanently from the database. Which SQL statement category should you use, and which command is correct?Solution
Step 1: Understand the goal
Removing a table and its data permanently means deleting the structure, not just data.Step 2: Identify correct category and command
DDL commands modify structure; DROP TABLE removes the table entirely.Final Answer:
DDL - DROP TABLE orders; -> Option BQuick Check:
Drop table = remove structure and data [OK]
- Using DELETE which only removes data, not table
- Confusing REVOKE with deleting tables
- Thinking SELECT can remove anything
Ready to prove it?
Pass the test and this pattern moves unknown
