Categories

Advertisement
⏱️ 8 min read

What is DQL

N
By NotesMind
Advertisement

What is DQL?

Definition (English)

DQL (Data Query Language) is a category of SQL commands used to retrieve data from one or more database tables.

Definition (Hindi)

DQL (Data Query Language) SQL का वह भाग है जिसका उपयोग Database से Data को पढ़ने (Retrieve), देखने (View) और खोजने (Search) के लिए किया जाता है।
 

Simple Definition

DQL = Read Data from Database

DDL → Creates Structure

DML → Changes Data

DQL → Reads Data


Real-Life Example

Suppose a college database contains 50,000 students.

You want to find:

  • All CSE students
  • Students whose marks are above 80
  • Students sorted by marks
  • Top 10 students

Instead of searching manually, SQL retrieves the data instantly.

Sample Table

We'll use this table throughout the chapter.

StudentID Name Age Department Marks
101 Rahul 20 CSE 85
102 Aman 21 IT 78
103 Neha 19 ECE 92
104 Priya 20 CSE 88
105 Rohit 22 ME 70

 

SELECT Statement

What is SELECT?

The SELECT statement retrieves data from one or more tables.

Syntax  

SELECT column_name
FROM table_name;

Select All Columns  

SELECT *
FROM Student;
StudentID Name Age Department Marks
101 Rahul 20 CSE 85
102 Aman 21 IT 78
103 Neha 19 ECE 92
104 Priya 20 CSE 88
105 Rohit 22 ME 70

* means all columns.


Select Specific Columns

SELECT Name, Marks
FROM Student;
Name Marks
Rahul 85
Aman 78
Neha 92
Priya 88
Rohit 70

FROM Clause

What is FROM?

The FROM clause specifies the table from which data will be retrieved.

SELECT Name
FROM Student;

Syntax

SELECT Name
FROM Student;

Here,

  • SELECT → What data?
  • FROM → From which table?

WHERE Clause

What is WHERE?

The WHERE clause filters records based on a condition.

Syntax

SELECT *
FROM Student
WHERE condition;

 

Example 1


 
SELECT *
FROM Student
WHERE Department='CSE';

Result

Name Department
Rahul CSE
Priya CSE

Example 2


 
SELECT *
FROM Student
WHERE Marks>80;

Result

Name Marks
Rahul 85
Neha 92
Priya 88

Comparison Operators

Operator Meaning
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
!= or <> Not equal

Example


 
SELECT *
FROM Student
WHERE Age>=20;

DISTINCT Keyword

What is DISTINCT?

DISTINCT removes duplicate values.

Suppose Department column contains:

Department
CSE
IT
ECE
CSE
IT

Query


 
SELECT DISTINCT Department
FROM Student;

Result

Department
CSE
IT
ECE

ORDER BY Clause

What is ORDER BY?

ORDER BY sorts the result.

Ascending Order (Default)


 
SELECT *
FROM Student
ORDER BY Marks ASC;

Result

70 → 78 → 85 → 88 → 92


Descending Order


 
SELECT *
FROM Student
ORDER BY Marks DESC;

Result

92 → 88 → 85 → 78 → 70


Order by Multiple Columns


 
SELECT *
FROM Student
ORDER BY Department ASC,
Marks DESC;

First sorts by Department, then by Marks within each department.


LIMIT Clause (MySQL/PostgreSQL)

What is LIMIT?

LIMIT returns only a specified number of rows.


 
SELECT *
FROM Student
LIMIT 3;

Returns the first 3 records.


SQL Server Equivalent


 
SELECT TOP 3 *
FROM Student;

Column Alias (AS)

What is Alias?

Alias gives a temporary name to a column.


 
SELECT Name AS StudentName,
Marks AS Score
FROM Student;

Result

StudentName Score
Rahul 85
Aman 78

Arithmetic Expressions

SQL can perform calculations.


 
SELECT Name,
Marks+5 AS NewMarks
FROM Student;

Result

Name NewMarks
Rahul 90
Aman 83

Logical Operators

AND

Both conditions must be true.


 
SELECT *
FROM Student
WHERE Department='CSE'
AND Marks>80;

OR

At least one condition must be true.


 
SELECT *
FROM Student
WHERE Department='IT'
OR Department='ECE';

NOT

Reverses the condition.


 
SELECT *
FROM Student
WHERE NOT Department='CSE';

IN Operator

Used to match multiple values.


 
SELECT *
FROM Student
WHERE Department IN ('CSE','IT');

Equivalent to


 
Department='CSE'
OR Department='IT'

BETWEEN Operator

Used to select values within a range.


 
SELECT *
FROM Student
WHERE Marks BETWEEN 80 AND 90;

Result

Rahul (85)

Priya (88)


LIKE Operator

Used for pattern matching.

Starts With


 
SELECT *
FROM Student
WHERE Name LIKE 'R%';

Matches

Rahul

Rohit


Ends With


 
SELECT *
FROM Student
WHERE Name LIKE '%a';

Matches

Neha


Contains


 
SELECT *
FROM Student
WHERE Name LIKE '%oh%';

Matches

Rohit


Wildcards

Wildcard Meaning
% Any number of characters
_ Exactly one character

Examples


 
LIKE 'A%'

Starts with A


 
LIKE '%n'

Ends with n


 
LIKE '_a%'

Second letter is "a"


IS NULL

Find NULL values.


 
SELECT *
FROM Student
WHERE Marks IS NULL;

IS NOT NULL


 
SELECT *
FROM Student
WHERE Marks IS NOT NULL;

Complete Example


 
SELECT Name,
Marks
FROM Student
WHERE Department='CSE'
AND Marks>80
ORDER BY Marks DESC;

Execution Flow


 
Student Table
      │
      ▼
WHERE Department='CSE'
      │
      ▼
Marks > 80
      │
      ▼
ORDER BY Marks DESC
      │
      ▼
Display Name and Marks

Common Mistakes

Missing Quotes

Incorrect


 
WHERE Name=Rahul

Correct


 
WHERE Name='Rahul'

Using = with NULL

Incorrect


 
WHERE Marks=NULL

Correct


 
WHERE Marks IS NULL

Forgetting ORDER BY Direction


 
ORDER BY Marks

Default is ASC.

Advertisement

💬 Leave a Comment & Rating