Skip to content
C

DQL


DQL

Definition

DQL (Data Query Language) is the single-statement family — really just SELECT — used to read data back out of the database without changing anything. Some textbooks fold DQL into DML since SELECT doesn't modify data either way; treating it as its own category emphasizes that reading and writing data are conceptually different jobs.

How It Works

SELECT is the most-used, most expressive statement in SQL, built from the clauses covered in SQL Statement Structure:

sql
SELECT department, SUM(salary) DIV COUNT(*) AS avg_salary FROM employees WHERE department <> 'Contractors' GROUP BY department HAVING COUNT(*) >= 2 ORDER BY avg_salary DESC LIMIT 5;

This single statement filters rows (WHERE), groups them (GROUP BY), computes an aggregate per group (SUM/COUNT), filters groups (HAVING), sorts the result (ORDER BY), and caps how many rows come back (LIMIT) — all without ever touching the underlying data. Running the same SELECT a thousand times produces a thousand identical reads and zero side effects, a property called being read-only or idempotent in the safest sense.

Edge Cases and Pitfalls

  • SELECT * (all columns) is convenient while exploring data interactively, but is considered poor practice in application code: it silently returns extra columns (and extra network/memory cost) if the table gains new columns later, and it breaks if a queried column is ever renamed or removed. Naming exact columns is the safer, intention-revealing habit.
  • A SELECT with a JOIN that has an unintended many-to-many relationship can silently multiply rows (a form of "fan-out"), producing a result that looks plausible but double-counts or over-counts values — this is one of the most common sources of subtly wrong aggregate results.
  • LIMIT without a corresponding ORDER BY returns some n rows, but which n rows is not guaranteed to be stable across runs — LIMIT only produces a deterministic, meaningful result when paired with an ORDER BY that fully determines row order.

Key Takeaways

  • DQL is fundamentally SELECT — a read-only statement that never modifies data, however elaborate its clauses become.
  • Prefer explicit column lists over SELECT * in real application code.
  • LIMIT needs ORDER BY to be meaningful and reproducible; otherwise "the first N rows" is an arbitrary, unstable choice made by the engine.

Mock Test

  • DQL - Quick Test

    8 questions on DQL.

    8 questions · 8 min · Medium
    Start Mock Test

Coding Problems