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?"