DQL stands for “Data Query Language,” and it is a subset of SQL (Structured Query Language) that focuses on retrieving data from a database. DQL allows you to formulate queries to retrieve specific information from one or more tables in a relational database management system (RDBMS).

The primary statement used in DQL is the SELECT statement. This statement enables you to specify which columns you want to retrieve, the tables you want to retrieve data from, and any conditions or criteria for selecting the data. Here’s a basic example of a SELECT statement:

SELECT column1, column2
FROM table_name
WHERE condition;

In this statement:

  • column1, column2: Columns you want to retrieve data from.
  • table_name: The table from which you want to retrieve data.
  • condition: Optional conditions that data must meet to be selected.

You can also use various clauses and keywords to refine your query, such as:

  • DISTINCT: Selects unique values only.
  • ORDER BY: Sorts the results based on specified columns.
  • GROUP BY: Groups results based on specified columns.
  • HAVING: Filters results after a GROUP BY operation.
  • LIMIT: Limits the number of results returned.

Here’s an example of a more complex query that combines several of these elements:

SELECT department, AVG(salary) AS avg_salary
FROM employees
WHERE department IN ('HR', 'Finance')
GROUP BY department
HAVING avg_salary > 50000
ORDER BY avg_salary DESC;

This query retrieves the average salary for employees in the HR and Finance departments, groups the results by department, filters out departments with an average salary less than or equal to 50000, and sorts the results in descending order of average salary.

DQL is a fundamental part of SQL and plays a crucial role in retrieving relevant data from databases for analysis, reporting, and decision-making.