Bird
Raised Fist0
SQLquery~5 mins

SQL statement categories (DDL, DML, DQL, DCL) - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: SQL statement categories (DDL, DML, DQL, DCL)
O(n) for DML; O(1) for DDL and DCL; O(log n) for DQL with index, otherwise O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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 Execution Grows With Input

How does time grow when data or structure size grows?

Input Size (n)Approx. Operations
10 rowsDDL: constant, DML: 10 inserts, DQL: search 10 rows, DCL: constant
100 rowsDDL: constant, DML: 100 inserts, DQL: search 100 rows, DCL: constant
1000 rowsDDL: 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.

Final Time Complexity

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.

Common Mistake

[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.

Interview Connect

Knowing how different SQL statements scale helps you write efficient queries and understand database behavior in real projects.

Self-Check

"What if a SELECT query uses an index? How would that affect its time complexity?"

Practice

(1/5)
1. Which category of SQL statements is used to create or modify the structure of database objects like tables and indexes?
easy
A. Data Manipulation Language (DML)
B. Data Query Language (DQL)
C. Data Definition Language (DDL)
D. Data Control Language (DCL)

Solution

  1. Step 1: Understand the role of DDL

    DDL commands define or change database structures, such as creating tables or indexes.
  2. Step 2: Differentiate from other categories

    DML changes data, DQL queries data, and DCL controls access, so they don't modify structure.
  3. Final Answer:

    Data Definition Language (DDL) -> Option C
  4. Quick Check:

    Structure changes = DDL [OK]
Hint: Structure changes mean DDL commands [OK]
Common Mistakes:
  • Confusing DDL with DML because both change something
  • Thinking SELECT is DDL instead of DQL
  • Mixing up DCL with DDL
2. Which of the following is the correct syntax to grant SELECT permission on a table named employees to a user john?
easy
A. GRANT SELECT TO john ON employees;
B. GRANT john ON employees SELECT;
C. SELECT GRANT ON employees TO john;
D. GRANT SELECT ON employees TO john;

Solution

  1. Step 1: Recall correct GRANT syntax

    The correct syntax is: GRANT permission ON object TO user;
  2. Step 2: Match syntax with options

    GRANT SELECT ON employees TO john; matches the correct order and keywords exactly.
  3. Final Answer:

    GRANT SELECT ON employees TO john; -> Option D
  4. Quick Check:

    GRANT + permission + ON + object + TO + user [OK]
Hint: GRANT syntax: GRANT permission ON object TO user [OK]
Common Mistakes:
  • Swapping TO and ON keywords
  • Placing user before permission
  • Using SELECT before GRANT
3. What will be the output of the following SQL statement?
SELECT * FROM employees WHERE salary > 50000;
medium
A. All rows from employees with salary greater than 50000
B. All rows from employees with salary less than 50000
C. Syntax error due to missing semicolon
D. No rows because salary column does not exist

Solution

  1. Step 1: Understand the SELECT query

    The query selects all columns (*) from employees where salary is greater than 50000.
  2. Step 2: Analyze the WHERE condition

    Only rows with salary > 50000 are returned; others are excluded.
  3. Final Answer:

    All rows from employees with salary greater than 50000 -> Option A
  4. Quick Check:

    WHERE salary > 50000 filters rows correctly [OK]
Hint: WHERE filters rows; > means greater than [OK]
Common Mistakes:
  • Confusing > with <
  • Thinking semicolon is mandatory for output
  • Assuming salary column missing without info
4. Identify the error in the following SQL statement:
INSERT INTO employees (id, name salary) VALUES (1, 'Alice', 60000);
medium
A. Missing comma between columns 'name' and 'salary'
B. VALUES keyword is misspelled
C. Table name should be in quotes
D. Cannot insert numeric value into salary column

Solution

  1. Step 1: Check column list syntax

    Columns must be separated by commas; here 'name' and 'salary' lack a comma.
  2. Step 2: Verify other parts

    VALUES is correct, table name does not need quotes, numeric salary is valid.
  3. Final Answer:

    Missing comma between columns 'name' and 'salary' -> Option A
  4. Quick Check:

    Column list commas are required [OK]
Hint: Check commas between column names carefully [OK]
Common Mistakes:
  • Ignoring missing commas in column list
  • Thinking table names need quotes
  • Assuming VALUES keyword typo
5. You want to remove a table named orders and all its data permanently from the database. Which SQL statement category should you use, and which command is correct?
hard
A. DML - DELETE FROM orders;
B. DDL - DROP TABLE orders;
C. DCL - REVOKE ALL ON orders;
D. DQL - SELECT * FROM orders;

Solution

  1. Step 1: Understand the goal

    Removing a table and its data permanently means deleting the structure, not just data.
  2. Step 2: Identify correct category and command

    DDL commands modify structure; DROP TABLE removes the table entirely.
  3. Final Answer:

    DDL - DROP TABLE orders; -> Option B
  4. Quick Check:

    Drop table = remove structure and data [OK]
Hint: DROP removes table; DELETE removes data only [OK]
Common Mistakes:
  • Using DELETE which only removes data, not table
  • Confusing REVOKE with deleting tables
  • Thinking SELECT can remove anything
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard