Skip to main content

Command Palette

Search for a command to run...

DQL order

Published
•5 min read•View as Markdown

Perfect 👍 — let’s go step-by-step to clearly understand DQL (Data Query Language) in SQL.


🔹 What is DQL?

DQL (Data Query Language) is used to fetch data from the database.
👉 It mainly deals with the SELECT statement.

Main Goal:

Retrieve data from one or more database tables using specific conditions, sorting, or grouping.


🔹 Common DQL Clauses

ClauseDescriptionExample Purpose
SELECTChoose columns to displaySELECT name, salary
FROMTable to query data fromFROM employees
WHEREFilter rows based on conditionsWHERE salary > 50000
ORDER BYSort result setORDER BY salary DESC
GROUP BYGroup rows by column valuesGROUP BY department
HAVINGFilter groups (like WHERE for groups)HAVING COUNT(*) > 5
DISTINCTRemove duplicate valuesSELECT DISTINCT department

🔹 5 Practical DQL Examples

🧩 Example 1: Basic SELECT

SELECT * 
FROM employees;

➡️ Use: Fetch all columns and rows from the employees table.


🧩 Example 2: SELECT with WHERE

SELECT emp_id, emp_name, salary 
FROM employees 
WHERE salary > 50000;

➡️ Use: Get employee ID, name, and salary of employees earning more than 50,000.


🧩 Example 3: ORDER BY

SELECT emp_name, department, salary 
FROM employees 
ORDER BY salary DESC;

➡️ Use: List employees with the highest salary first.


🧩 Example 4: GROUP BY + HAVING

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

➡️ Use: Find departments that have more than 5 employees.


🧩 Example 5: DISTINCT

SELECT DISTINCT department
FROM employees;

➡️ Use: Get all unique department names from the employees table.


🔹 Summary

CategoryCommandUse
DQL CommandSELECTRetrieve data
Supporting ClausesWHERE, GROUP BY, HAVING, ORDER BY, DISTINCTFilter, group, sort, and clean results
Used InReports, dashboards, data analytics, automation verification queries

✅ In automation testing, you might use DQL queries in JDBC or API-based testing to verify database values (e.g., after placing an order, check data consistency).


Would you like me to show how to run these SQL queries in Java (JDBC) to verify data automatically from your automation framework?

You said:

order of execution

ChatGPT said:

Excellent question 👏 —
understanding the order of SQL query execution (a.k.a. logical query processing order) is very important, especially when working with SELECT queries and automation verifications.

Let’s break it down clearly 👇


🔹 SQL Query Logical Execution Order

When you write a query like this:

SELECT department, COUNT(*) AS total
FROM employees
WHERE salary > 50000
GROUP BY department
HAVING COUNT(*) > 3
ORDER BY total DESC;

You might think SQL runs from SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY,
but in reality, it executes in this logical order 👇

StepClausePurpose
1️⃣FROMIdentify and join the tables involved.
2️⃣WHEREFilter rows (conditions on individual rows).
3️⃣GROUP BYGroup remaining rows based on column values.
4️⃣HAVINGFilter groups (conditions on aggregated results).
5️⃣SELECTChoose columns or calculated fields to display.
6️⃣ORDER BYSort the final result set.
7️⃣LIMIT / OFFSET (optional)Restrict number of rows returned.

🧠 Visualization of Flow

FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

🔹 Example with Explanation

Query:

SELECT department, AVG(salary) AS avg_sal
FROM employees
WHERE job_title = 'Engineer'
GROUP BY department
HAVING AVG(salary) > 60000
ORDER BY avg_sal DESC;

Step-by-step execution:

StepClauseWhat Happens
1️⃣FROMLoads table employees
2️⃣WHEREFilters only rows where job_title = 'Engineer'
3️⃣GROUP BYGroups the filtered data by department
4️⃣HAVINGKeeps only departments whose avg(salary) > 60000
5️⃣SELECTDisplays department name and avg salary
6️⃣ORDER BYSorts the results by avg salary descending

🔹 Important Notes

  • WHERE works before aggregation (can’t use aggregate functions there).

  • HAVING works after aggregation (used with functions like COUNT, AVG).

  • SELECT comes after grouping, meaning aliases defined in SELECT can be used in ORDER BY, but not in WHERE.


🔹 Automation Tip

In automation testing (especially database validation):

  • Always remember filtering using WHERE happens before grouping.

  • If you’re validating aggregate results (like sales total), use HAVING to compare expected aggregated values.


✅ In short:

Logical OrderClause
1FROM
2WHERE
3GROUP BY
4HAVING
5SELECT
6ORDER BY
7LIMIT / OFFSET