Page Button

Dual

๐ŸŽ FREE Physics Notes & Govt Job Alerts
✔ 100% Free
๐Ÿ” Search Saral Physics Resources

Friday, August 7, 2026

Complete SQL Data Filtering Guide: WHERE, IN, BETWEEN, LIKE & NULL Explained

SQL Filtering Data (WHERE, AND, OR, IN, BETWEEN, LIKE, IS NULL) – Ultimate Guide

SQL Filtering Data: The Ultimate Beginner's Guide (WHERE, AND, OR, IN, BETWEEN, LIKE, IS NULL)

1. Sample Database Setup (เค›ाเคค्เคฐ เค”เคฐ เค•เคฐ्เคฎเคšाเคฐी เคคाเคฒिเค•ाเคं)

Before learning how to filter data, we need sample data to run our queries on. Throughout this entire guide, we will use two primary tables: Students and Employees.
เคกेเคŸा เค•ो เคซ़िเคฒ्เคŸเคฐ เค•เคฐเคจा เคธीเค–เคจे เคธे เคชเคนเคฒे, เคนเคฎें เค…เคชเคจी เค•्เคตेเคฐीเคœ़ เคšเคฒाเคจे เค•े เคฒिเค เคธैंเคชเคฒ เคกेเคŸा เค•ी เค†เคตเคถ्เคฏเค•เคคा เคนोเคคी เคนै। เค‡เคธ เคชूเคฐे เค—ाเค‡เคก เคฎें, เคนเคฎ เคฆो เคฎुเค–्เคฏ เคคाเคฒिเค•ाเค“ं เค•ा เค‰เคชเคฏोเค— เค•เคฐेंเค—े: Students เค”เคฐ Employees

Table 1: Students

StudentID FirstName LastName Age Grade City Marks
1RahulSharma1510Delhi85.5
2PriyaVerma149Mumbai92.0
3AmitPatel1610AhmedabadNULL
4NehaSingh1510Delhi68.0
5RohanGupta149Bangalore74.5
6AnanyaDas1611Kolkata95.0

Table 2: Employees

EmpID EmpName Department Salary JoiningDate ManagerID
101Aarav MehtaIT750002021-03-15NULL
102Sanya MalhotraHR500002022-06-01101
103Vikram RathoreIT850002020-01-10101
104Kavita RoyFinance600002023-08-20102
105Rajesh KumarSales450002022-11-12102

2. SQL WHERE Clause (เคกेเคŸा เคซ़िเคฒ्เคŸเคฐ เค•เคฐเคจा)

The WHERE clause is used to filter records from a database table based on a specific condition. It extracts only those records that satisfy the specified condition.
WHERE เค•्เคฒॉเคœ เค•ा เค‰เคชเคฏोเค— เค•िเคธी เคตिเคถिเคท्เคŸ เคถเคฐ्เคค (condition) เค•े เค†เคงाเคฐ เคชเคฐ เคกेเคŸाเคฌेเคธ เคŸेเคฌเคฒ เคธे เคฐिเค•ॉเคฐ्เคก्เคธ เค•ो เคซ़िเคฒ्เคŸเคฐ เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै। เคฏเคน เค•ेเคตเคฒ เค‰เคจ्เคนीं เคฐिเค•ॉเคฐ्เคก्เคธ เค•ो เคจिเค•ाเคฒเคคा เคนै เคœो เคจिเคฐ्เคฆिเคท्เคŸ เคถเคฐ्เคค เค•ो เคชूเคฐा เค•เคฐเคคे เคนैं।
Real-Life Analogy (เคตाเคธ्เคคเคตिเค• เคœीเคตเคจ เค•ा เค‰เคฆाเคนเคฐเคฃ)
Imagine asking a school teacher: "Show me the list of students who live in Delhi." You don't want all students; you only want students filtered by city = 'Delhi'. That filter is the WHERE clause.
เคธोเคšिเค เค•ि เค†เคช เคเค• เคถिเค•्เคทเค• เคธे เคชूเค›เคคे เคนैं: "เคฎुเคे เค‰เคจ เค›ाเคค्เคฐों เค•ी เคธूเคšी เคฆिเค–ाเคं เคœो เคฆिเคฒ्เคฒी เคฎें เคฐเคนเคคे เคนैं।" เค†เคช เคธเคญी เค›ाเคค्เคฐ เคจเคนीं เคšाเคนเคคे; เค†เคช เค•ेเคตเคฒ เคถเคนเคฐ = 'เคฆिเคฒ्เคฒी' เคธे เคซ़िเคฒ्เคŸเคฐ เค•िเค เค—เค เค›ाเคค्เคฐ เคšाเคนเคคे เคนैं। เคตเคนी เคซ़िเคฒ्เคŸเคฐ WHERE เค•्เคฒॉเคœ เคนै।

Syntax (เคธिंเคŸैเค•्เคธ)

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example 1: Basic Filtering

Select all students who are in Grade 10.
เค‰เคจ เคธเคญी เค›ाเคค्เคฐों เค•ो เคšुเคจें เคœो เค•เค•्เคทा 10 (Grade 10) เคฎें เคนैं।
SELECT * 
FROM Students 
WHERE Grade = 10;
StudentID FirstName LastName Age Grade City Marks
1RahulSharma1510Delhi85.5
3AmitPatel1610AhmedabadNULL
4NehaSingh1510Delhi68.0
Line-by-Line Explanation:
  • SELECT *: Retrieves all columns from the table.
  • FROM Students: Specifies the source table.
  • WHERE Grade = 10: Scans each row and keeps only those where the Grade column value equals 10.
เคชंเค•्เคคि-เคฆเคฐ-เคชंเค•्เคคि เคตिเคตเคฐเคฃ:
  • SELECT *: เคคाเคฒिเค•ा เคธे เคธเคญी เค•ॉเคฒเคฎ เคช्เคฐाเคช्เคค เค•เคฐเคคा เคนै।
  • FROM Students: เคธ्เคฐोเคค เคคाเคฒिเค•ा เคจिเคฐ्เคฆिเคท्เคŸ เค•เคฐเคคा เคนै।
  • WHERE Grade = 10: เคช्เคฐเคค्เคฏेเค• เคชंเค•्เคคि เค•ी เคœाँเคš เค•เคฐเคคा เคนै เค”เคฐ เค•ेเคตเคฒ เค‰เคจ्เคนीं เค•ो เคฐเค–เคคा เคนै เคœเคนाँ Grade เค•ॉเคฒเคฎ เค•ा เคฎाเคจ 10 เค•े เคฌเคฐाเคฌเคฐ เคนै।
Common Mistake (เคธाเคฎाเคจ्เคฏ เค—เคฒเคคी)
Forgetting single quotes around string/text values. Numbers do not need quotes, but text values do. Example: WHERE City = 'Delhi' is correct; WHERE City = Delhi will throw an error.
เคธ्เคŸ्เคฐिंเค—/เคŸेเค•्เคธ्เคŸ เคฎाเคจों เค•े เคšाเคฐों เค“เคฐ เคธिंเค—เคฒ เค•ोเคŸ (single quotes) เคฒเค—ाเคจा เคญूเคฒเคจा। เคธंเค–्เคฏाเค“ं เค•ो เค‰เคฆ्เคงเคฐเคฃों เค•ी เค†เคตเคถ्เคฏเค•เคคा เคจเคนीं เคนोเคคी เคนै, เคฒेเค•िเคจ เคŸेเค•्เคธ्เคŸ เคฎाเคจों เค•ो เคนोเคคी เคนै। เค‰เคฆाเคนเคฐเคฃ: WHERE City = 'Delhi' เคธเคนी เคนै; WHERE City = Delhi เคเค• เคเคฐเคฐ เคฆेเค—ा।
Interview Tip (เค‡ंเคŸเคฐเคต्เคฏू เคŸिเคช)
Remember that the WHERE clause executes before row grouping (GROUP BY) and before final projections (SELECT). It works on individual row-level data.
เคฏाเคฆ เคฐเค–ें เค•ि WHERE เค•्เคฒॉเคœ เคฐो เค—्เคฐुเคชिंเค— (GROUP BY) เคธे เคชเคนเคฒे เค”เคฐ เค…ंเคคिเคฎ เคช्เคฐोเคœेเค•्เคถเคจ (SELECT) เคธे เคชเคนเคฒे เคจिเคท्เคชाเคฆिเคค เคนोเคคा เคนै। เคฏเคน เคต्เคฏเค•्เคคिเค—เคค เคฐो-เคธ्เคคเคฐ (row-level) เค•े เคกेเคŸा เคชเคฐ เค•ाเคฎ เค•เคฐเคคा เคนै।

3. SQL AND Operator (เค”เคฐ เค‘เคชเคฐेเคŸเคฐ)

The AND operator is used to filter records based on more than one condition. It returns a record if ALL conditions separated by AND evaluate to TRUE.
AND เค‘เคชเคฐेเคŸเคฐ เค•ा เค‰เคชเคฏोเค— เคเค• เคธे เค…เคงिเค• เคถเคฐ्เคคों เค•े เค†เคงाเคฐ เคชเคฐ เคฐिเค•ॉเคฐ्เคก เค•ो เคซ़िเคฒ्เคŸเคฐ เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै। เคฏเคฆि AND เคฆ्เคตाเคฐा เค…เคฒเค— เค•ी เค—เคˆ เคธเคญी เคถเคฐ्เคคें TRUE (เคธเคค्เคฏ) เคนोเคคी เคนैं, เคคो เคฏเคน เคฐिเค•ॉเคฐ्เคก เคฒौเคŸाเคคा เคนै।

Syntax (เคธिंเคŸैเค•्เคธ)

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3;

Example: Multiple Conditions with AND

Find all students who are in Grade 10 AND live in 'Delhi'.
เค‰เคจ เคธเคญी เค›ाเคค्เคฐों เค•ो เค–ोเคœें เคœो เค•เค•्เคทा 10 เคฎें เคนैं เค”เคฐ 'เคฆिเคฒ्เคฒी' เคฎें เคฐเคนเคคे เคนैं।
SELECT * 
FROM Students 
WHERE Grade = 10 AND City = 'Delhi';
StudentID FirstName LastName Age Grade City Marks
1RahulSharma1510Delhi85.5
4NehaSingh1510Delhi68.0
Explanation: Student Amit Patel is in Grade 10, but lives in Ahmedabad. Since both conditions (Grade = 10 AND City = 'Delhi') were not true, Amit was excluded.
เคตिเคตเคฐเคฃ: เค›ाเคค्เคฐ เค…เคฎिเคค เคชเคŸेเคฒ เค•เค•्เคทा 10 เคฎें เคนैं, เคฒेเค•िเคจ เค…เคนเคฎเคฆाเคฌाเคฆ เคฎें เคฐเคนเคคे เคนैं। เคšूंเค•ि เคฆोเคจों เคถเคฐ्เคคें (Grade = 10 เค”เคฐ City = 'Delhi') เคธเคค्เคฏ เคจเคนीं เคฅीं, เค‡เคธเคฒिเค เค…เคฎिเคค เค•ो เคฌाเคนเคฐ เค•เคฐ เคฆिเคฏा เค—เคฏा เคฅा।

4. SQL OR Operator (เคฏा เค‘เคชเคฐेเคŸเคฐ)

The OR operator displays a record if ANY of the conditions separated by OR evaluates to TRUE.
OR เค‘เคชเคฐेเคŸเคฐ เค•िเคธी เคฐिเค•ॉเคฐ्เคก เค•ो เคคเคฌ เคช्เคฐเคฆเคฐ्เคถिเคค เค•เคฐเคคा เคนै เคœเคฌ OR เคฆ्เคตाเคฐा เค…เคฒเค— เค•ी เค—เคˆ เคถเคฐ्เคคों เคฎें เคธे เค•ोเคˆ เคญी เคเค• เคถเคฐ्เคค TRUE (เคธเคค्เคฏ) เคนोเคคी เคนै।

Syntax (เคธिंเคŸैเค•्เคธ)

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2;

Example: OR Filtering

Find students who live in either 'Delhi' OR 'Mumbai'.
เค‰เคจ เค›ाเคค्เคฐों เค•ो เค–ोเคœें เคœो 'เคฆिเคฒ्เคฒी' เคฏा 'เคฎुंเคฌเคˆ' เคฎें เคฐเคนเคคे เคนैं।
SELECT * 
FROM Students 
WHERE City = 'Delhi' OR City = 'Mumbai';
StudentID FirstName LastName Age Grade City Marks
1RahulSharma1510Delhi85.5
2PriyaVerma149Mumbai92.0
4NehaSingh1510Delhi68.0
Operator Precedence: Combining AND & OR
In SQL, AND takes priority over OR. Always use parentheses () to group conditions correctly when using both operators together!
SQL เคฎें, AND เค•ो OR เคธे เค…เคงिเค• เคช्เคฐाเคฅเคฎिเค•เคคा เคฎिเคฒเคคी เคนै। เคฆोเคจों เค‘เคชเคฐेเคŸเคฐों เค•ा เคเค• เคธाเคฅ เค‰เคชเคฏोเค— เค•เคฐเคคे เคธเคฎเคฏ เคถเคฐ्เคคों เค•ो เคธเคนी เคขंเค— เคธे เคธเคฎूเคนเคฌเคฆ्เคง เค•เคฐเคจे เค•े เคฒिเค เคนเคฎेเคถा เค•ोเคท्เค เค• () เค•ा เค‰เคชเคฏोเค— เค•เคฐें!

5. SQL IN Operator (เค†เคˆเคเคจ เค‘เคชเคฐेเคŸเคฐ)

The IN operator allows you to specify multiple values in a WHERE clause. It acts as a shorthand for multiple OR conditions.
IN เค‘เคชเคฐेเคŸเคฐ เค†เคชเค•ो WHERE เค•्เคฒॉเคœ เคฎें เค•เคˆ เคฎाเคจों เค•ो เคจिเคฐ्เคฆिเคท्เคŸ เค•เคฐเคจे เค•ी เค…เคจुเคฎเคคि เคฆेเคคा เคนै। เคฏเคน เค•เคˆ OR เคถเคฐ्เคคों เค•े เคฒिเค เคเค• เค›ोเคŸा (shorthand) เคคเคฐीเค•ा เคนै।

Syntax (เคธिंเคŸैเค•्เคธ)

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Example: Clean Code with IN

Find students from Delhi, Mumbai, or Kolkata using IN operator.
IN เค‘เคชเคฐेเคŸเคฐ เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคฆिเคฒ्เคฒी, เคฎुंเคฌเคˆ เคฏा เค•ोเคฒเค•ाเคคा เค•े เค›ाเคค्เคฐों เค•ो เค–ोเคœें।
SELECT * 
FROM Students 
WHERE City IN ('Delhi', 'Mumbai', 'Kolkata');
Why use IN over OR?
  • Shorter and much easier to read.
  • Executes faster when searching through long lists of values.
  • Allows subqueries inside the list dynamically.
OR เค•े เคฌเคœाเคฏ IN เค•ा เค‰เคชเคฏोเค— เค•्เคฏों เค•เคฐें?
  • เค›ोเคŸा เค”เคฐ เคชเคข़เคจे เคฎें เคฌเคนुเคค เค†เคธाเคจ।
  • เคฎाเคจों เค•ी เคฒंเคฌी เคธूเคšी เค•े เคฎाเคง्เคฏเคฎ เคธे เค–ोเคœเคคे เคธเคฎเคฏ เคคेเคœी เคธे เคจिเคท्เคชाเคฆिเคค เคนोเคคा เคนै।
  • เคธूเคšी เค•े เคญीเคคเคฐ เค—เคคिเคถीเคฒ (dynamically) เคฐूเคช เคธे เคธเคฌเค•्เคตेเคฐी เค•ी เค…เคจुเคฎเคคि เคฆेเคคा เคนै।

6. SQL BETWEEN Operator (เคฌीเคŸเคตीเคจ เค‘เคชเคฐेเคŸเคฐ)

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. It is inclusive, meaning both the start and end values are included in the results.
BETWEEN เค‘เคชเคฐेเคŸเคฐ เคเค• เคฆी เค—เคˆ เคธीเคฎा (range) เค•े เคญीเคคเคฐ เคฎाเคจों เค•ा เคšเคฏเคจ เค•เคฐเคคा เคนै। เคฎाเคจ เคธंเค–्เคฏाเคं, เคชाเค  (text), เคฏा เคคिเคฅिเคฏां เคนो เคธเค•เคคी เคนैं। เคฏเคน เคธเคฎाเคตेเคถी (inclusive) เคนै, เคœिเคธเค•ा เค…เคฐ्เคฅ เคนै เค•ि เคช्เคฐाเคฐंเคญ เค”เคฐ เค…ंเคค เคฆोเคจों เคฎाเคจ เคชเคฐिเคฃाเคฎों เคฎें เคถाเคฎिเคฒ เคนैं।

Syntax (เคธिंเคŸैเค•्เคธ)

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example 1: Numeric Range

Find employees earning between 50,000 and 80,000.
50,000 เค”เคฐ 80,000 เค•े เคฌीเคš เค•เคฎाเคจे เคตाเคฒे เค•เคฐ्เคฎเคšाเคฐिเคฏों เค•ो เค–ोเคœें।
SELECT * 
FROM Employees 
WHERE Salary BETWEEN 50000 AND 80000;

Example 2: Date Range Filtering

Find employees who joined between January 1, 2021, and December 31, 2022.
1 เคœเคจเคตเคฐी 2021 เค”เคฐ 31 เคฆिเคธंเคฌเคฐ 2022 เค•े เคฌीเคš เคถाเคฎिเคฒ เคนोเคจे เคตाเคฒे เค•เคฐ्เคฎเคšाเคฐिเคฏों เค•ो เค–ोเคœें।
SELECT * 
FROM Employees 
WHERE JoiningDate BETWEEN '2021-01-01' AND '2022-12-31';

7. SQL LIKE Operator & Wildcards (เคฒाเค‡เค• เค‘เคชเคฐेเคŸเคฐ เค”เคฐ เคตाเค‡เคฒ्เคกเค•ाเคฐ्เคก)

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
LIKE เค‘เคชเคฐेเคŸเคฐ เค•ा เค‰เคชเคฏोเค— WHERE เค•्เคฒॉเคœ เคฎें เค•ॉเคฒเคฎ เคฎें เค•िเคธी เคตिเคถिเคท्เคŸ เคชैเคŸเคฐ्เคจ เค•ी เค–ोเคœ เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।

Wildcard Characters Table

Wildcard Description (English) เคตिเคตเคฐเคฃ (Hindi) Example Query Pattern
% Represents zero, one, or multiple characters เคถूเคจ्เคฏ, เคเค•, เคฏा เค•เคˆ เคตเคฐ्เคฃों เค•ो เคฆเคฐ्เคถाเคคा เคนै 'a%' (Starts with 'a')
_ Represents a single specified character เคเค• เคเค•เคฒ เคตिเคถिเคท्เคŸ เคตเคฐ्เคฃ เค•ो เคฆเคฐ्เคถाเคคा เคนै '_a%' ('a' in second position)

Real-Life Search Examples

  • WHERE FirstName LIKE 'A%': Names starting with 'A' (e.g., Aarav, Amit, Ananya).
  • WHERE FirstName LIKE '%a': Names ending with 'a' (e.g., Priya, Neha, Sanya).
  • WHERE FirstName LIKE '%ha%': Names containing 'ha' anywhere (e.g., Rahul, Neha).
  • WHERE FirstName LIKE '_r%': Names with 'r' as second letter (e.g., Priya).
  • WHERE FirstName LIKE 'A%': 'A' เคธे เคถुเคฐू เคนोเคจे เคตाเคฒे เคจाเคฎ (เคœैเคธे, Aarav, Amit, Ananya)।
  • WHERE FirstName LIKE '%a': 'a' เคชเคฐ เคธเคฎाเคช्เคค เคนोเคจे เคตाเคฒे เคจाเคฎ (เคœैเคธे, Priya, Neha, Sanya)।
  • WHERE FirstName LIKE '%ha%': เคจाเคฎ เคœिเคจเคฎें เค•เคนीं เคญी 'ha' เคถाเคฎिเคฒ เคนो (เคœैเคธे, Rahul, Neha)।
  • WHERE FirstName LIKE '_r%': เคจाเคฎ เคœिเคจเคฎें เค…เค•्เคทเคฐ 'r' เคฆूเคธเคฐे เคธ्เคฅाเคจ เคชเคฐ เคนो (เคœैเคธे, Priya)।

8. SQL IS NULL & IS NOT NULL (เคจเคฒ เคฎाเคจ เคซ़िเคฒ्เคŸเคฐिंเค—)

A field with a NULL value is a field with no value. It represents missing or unknown data. You cannot use arithmetic comparison operators such as = or <> to test for NULL. You must use IS NULL or IS NOT NULL.
NULL เคฎाเคจ เคตाเคฒा เคซ़ीเคฒ्เคก เคฌिเคจा เค•िเคธी เคฎाเคจ เคตाเคฒा เคซ़ीเคฒ्เคก เคนै। เคฏเคน เค—ाเคฏเคฌ เคฏा เค…เคœ्เคžाเคค เคกेเคŸा เค•ा เคช्เคฐเคคिเคจिเคงिเคค्เคต เค•เคฐเคคा เคนै। เค†เคช NULL เค•ी เคœाँเคš เค•े เคฒिเค = เคฏा <> เคœैเคธे เค—เคฃिเคคीเคฏ เคคुเคฒเคจा เค‘เคชเคฐेเคŸเคฐों เค•ा เค‰เคชเคฏोเค— เคจเคนीं เค•เคฐ เคธเค•เคคे। เค†เคชเค•ो IS NULL เคฏा IS NOT NULL เค•ा เค‰เคชเคฏोเค— เค•เคฐเคจा เคนोเค—ा।

Example: Finding Missing Data

Find all students whose test marks were NOT recorded (missing marks).
เค‰เคจ เคธเคญी เค›ाเคค्เคฐों เค•ो เค–ोเคœें เคœिเคจเค•े เคชเคฐीเค•्เคทा เค…ंเค• เคฆเคฐ्เคœ เคจเคนीं เค•िเค เค—เค เคฅे (เคฒाเคชเคคा เค…ंเค•)।
SELECT * 
FROM Students 
WHERE Marks IS NULL;
StudentID FirstName LastName Age Grade City Marks
3AmitPatel1610AhmedabadNULL
Critical Mistake (เค—ंเคญीเคฐ เค—เคฒเคคी)
Never write WHERE Marks = NULL. In SQL standard SQL, comparing anything to NULL using '=' yields UNKNOWN, returning zero rows!
เค•เคญी เคญी WHERE Marks = NULL เคจ เคฒिเค–ें। SQL เคฎाเคจเค• เคฎें, '=' เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เค•िเคธी เคญी เคšीเคœ़ เค•ी NULL เคธे เคคुเคฒเคจा เค•เคฐเคจे เคชเคฐ UNKNOWN เคช्เคฐाเคช्เคค เคนोเคคा เคนै, เคœिเคธเคธे เคถूเคจ्เคฏ เคชंเค•्เคคिเคฏाँ เคฎिเคฒเคคी เคนैं!

9. Comprehensive Operator Comparison Tables

Comparison 1: WHERE vs HAVING

Feature WHERE Clause HAVING Clause
Scope Filters individual rows before grouping. Filters aggregated groups after GROUP BY.
Aggregate Functions Cannot contain aggregate functions (e.g., SUM, AVG). Can contain aggregate functions.
Performance Faster because it reduces dataset early. Evaluated later in statement processing.

Comparison 2: NULL vs Empty String ('')

Aspect NULL Value Empty String ('')
Meaning Absence of data / Unknown value. Known text value with zero length.
Memory Allocation Takes 0 bytes / special byte tracking. Occupies memory as a string.
SQL Test WHERE col IS NULL WHERE col = ''

10. Real-Life Project: School Management System Filtering

Let's look at how filter queries are implemented in actual software user interfaces for a School Management System.
เค†เค‡เค เคฆेเค–ें เค•ि เคธ्เค•ूเคฒ เคช्เคฐเคฌंเคงเคจ เคช्เคฐเคฃाเคฒी เค•े เคฒिเค เคตाเคธ्เคคเคตिเค• เคธॉเคซ़्เคŸเคตेเคฏเคฐ เคฏूเคœเคฐ เค‡ंเคŸเคฐเคซेเคธ เคฎें เคซ़िเคฒ्เคŸเคฐ เคช्เคฐเคถ्เคจों เค•ो เค•ैเคธे เคฒाเค—ू เค•िเคฏा เคœाเคคा เคนै।

Module A: Fee Default Tracking

Query to find all active students in Grade 10 from Delhi who have pending fee records or unassigned marks:
เคฆिเคฒ्เคฒी เค•े เค•เค•्เคทा 10 เค•े เค‰เคจ เคธเคญी เคธเค•्เคฐिเคฏ เค›ाเคค्เคฐों เค•ो เค–ोเคœเคจे เค•े เคฒिเค เคช्เคฐเคถ्เคจ เคœिเคจเค•े เคชाเคธ เคฒंเคฌिเคค เคถुเคฒ्เค• เคฐिเค•ॉเคฐ्เคก เคฏा เค…เคธाเค‡เคจ เคจ เค•िเค เค—เค เค…ंเค• เคนैं:
SELECT StudentID, FirstName, LastName, City, Marks
FROM Students
WHERE Grade = 10 
  AND City = 'Delhi' 
  AND (Marks IS NULL OR Marks < 70.0);

Module B: Honor Roll Student Identification

Find students who scored 85 or above, excluding missing scores, ordered from highest to lowest:
เคฒाเคชเคคा เค…ंเค•ों เค•ो เค›ोเคก़เค•เคฐ, เค‰เคš्เคšเคคเคฎ เคธे เคจिเคฎ्เคจเคคเคฎ เค•्เคฐเคฎ เคฎें 85 เคฏा เค‰เคธเคธे เค…เคงिเค• เค…ंเค• เคช्เคฐाเคช्เคค เค•เคฐเคจे เคตाเคฒे เค›ाเคค्เคฐों เค•ो เค–ोเคœें:
SELECT FirstName, LastName, Marks
FROM Students
WHERE Marks IS NOT NULL 
  AND Marks >= 85.0;

11. Key Takeaways Summary (เคฎुเค–्เคฏ เคตिเคšाเคฐ)

Operator Purpose Quick Syntax Example
WHEREFilter individual rows based on conditionWHERE Age > 18
ANDCombine multiple conditions; all must be TRUEWHERE Age > 18 AND Status = 'Active'
ORCombine conditions; at least one must be TRUEWHERE City = 'Delhi' OR City = 'Noida'
INMatch any value in a listWHERE Role IN ('Admin', 'Teacher')
BETWEENFilter values within inclusive rangeWHERE Salary BETWEEN 40000 AND 90000
LIKEPattern matching with % and _ wildcardsWHERE Name LIKE 'J%'
IS NULLCheck for missing/unassigned valuesWHERE ManagerID IS NULL

12. 25 Beginner SQL Interview Questions & Answers

  1. What is the primary purpose of the WHERE clause?
    Answer: It filters rows returned by a query based on specified conditional criteria.
  2. Can you use column aliases in a WHERE clause? Why or why not?
    Answer: No, because the WHERE clause is evaluated before the SELECT clause assigns aliases in SQL execution order.
  3. What is the difference between AND and OR operators?
    Answer: AND requires all conditions to be TRUE; OR requires only one condition to be TRUE.
  4. How does SQL handle comparison with NULL values?
    Answer: Standard comparisons (=, !=) return UNKNOWN. You must use IS NULL or IS NOT NULL.
  5. Is the BETWEEN operator inclusive or exclusive?
    Answer: It is inclusive. Both boundary values are included in the result.
  6. What does the % wildcard represent in a LIKE operator query?
    Answer: Zero, one, or multiple characters.
  7. What does the _ (underscore) wildcard represent?
    Answer: Exactly one single character.
  8. Write a query to find names ending with 'son'.
    Answer: SELECT * FROM Users WHERE Name LIKE '%son';
  9. What is the performance advantage of IN over multiple OR conditions?
    Answer: IN is cleaner to write and databases optimize IN lists using index lookups better.
  10. How do you negate a BETWEEN condition?
    Answer: By using NOT BETWEEN.
  11. What is the difference between WHERE and HAVING?
    Answer: WHERE filters individual rows before grouping; HAVING filters aggregated groups.
  12. Does SQL LIKE matching case-sensitive?
    Answer: It depends on database collation (e.g., MySQL default is case-insensitive, PostgreSQL LIKE is case-sensitive).
  13. How to test if a string is empty vs NULL?
    Answer: Use WHERE col = '' for empty strings and WHERE col IS NULL for NULL values.
  14. Can we use multiple AND and OR conditions in one query?
    Answer: Yes, using parentheses to control evaluation order.
  15. What happens if both sides of an AND condition are NULL?
    Answer: The expression evaluates to NULL (UNKNOWN).
  16. What query returns employees who do NOT have a manager assigned?
    Answer: SELECT * FROM Employees WHERE ManagerID IS NULL;
  17. How do you perform wildcards literal search if text has '%'?
    Answer: Use an ESCAPE character, e.g., LIKE '10\%' ESCAPE '\'.
  18. What operator is used to search for values within a set returned by a subquery?
    Answer: The IN or EXISTS operator.
  19. Is WHERE 1=1 valid SQL syntax?
    Answer: Yes, it always evaluates to TRUE and is often used in dynamic query builders.
  20. Which operator is best suited for date range queries?
    Answer: The BETWEEN operator or logical operators (>= AND <=).
  21. What is the evaluation order of NOT, AND, and OR?
    Answer: NOT has the highest precedence, followed by AND, then OR.
  22. Can WHERE clause be used with UPDATE statements?
    Answer: Yes, to filter which rows get updated. Without WHERE, all rows are updated!
  23. Can WHERE clause be used with DELETE statements?
    Answer: Yes, to specify which records to delete.
  24. How to select records where a column value is NOT IN a list?
    Answer: Use WHERE column NOT IN (val1, val2).
  25. What is the output of SELECT * FROM Table WHERE NULL = NULL;?
    Answer: Zero rows, because NULL comparison yields UNKNOWN.

13. 25 Practice Questions (Abhyas Prashn)

Easy Level (เค†เคธाเคจ เคธ्เคคเคฐ)

  1. Write a SQL query to select all employees from the 'IT' department.
  2. Find all students who are 15 years old.
  3. Select all employees whose salary is greater than 60,000.
  4. List all students living in 'Mumbai'.
  5. Find all employees hired after '2022-01-01'.
  6. List all students whose marks are less than 75.
  7. Find employees with EmpID equal to 103.
  8. Select all students in Grade 9.

Medium Level (เคฎเคง्เคฏเคฎ เคธ्เคคเคฐ)

  1. Select students living in 'Delhi' AND having age equal to 15.
  2. Find employees working in 'IT' OR 'Finance' departments.
  3. Retrieve all students whose age is in the list (14, 16).
  4. Find all employees earning between 50,000 and 80,000 using BETWEEN.
  5. Select students whose FirstName starts with 'R'.
  6. Find all employees whose name ends with 'a'.
  7. Select all students whose marks value is currently missing (NULL).
  8. Retrieve employees who do NOT work in HR department.
  9. Select all students whose city is NOT IN ('Delhi', 'Mumbai').

Challenge Level (เค•เค िเคจ เคธ्เคคเคฐ)

  1. Find IT employees earning over 70,000 OR HR employees earning over 45,000.
  2. Select students whose second letter of FirstName is 'a'.
  3. Write a query using BETWEEN to select students aged 14 to 16 excluding those from 'Delhi'.
  4. Retrieve all employees whose JoiningDate is in year 2022 using LIKE.
  5. Find students whose Marks are NOT NULL and greater than 80.
  6. Write a query combining WHERE, IN, and LIKE to find students from 'Delhi' or 'Mumbai' whose name contains 'a'.
  7. Select employees whose ManagerID is NOT NULL and Department = 'IT'.
  8. Write a query to handle NULL marks by returning students who either have NULL marks OR marks below 40.

14. Frequently Asked Questions (FAQs)

Q1: How do I filter text data with case sensitivity in SQL?
In most SQL dialects (like MySQL), default text searches are case-insensitive. You can use the BINARY keyword (in MySQL) or COLLATE options to force case sensitivity.
Q2: What is the difference between IN and EXISTS in SQL filtering?
IN evaluates a literal list or subquery values, whereas EXISTS checks for the presence of rows returned by a subquery. EXISTS is generally faster for large datasets.
Q3: Why is my SQL BETWEEN query not including end-date values?
If your date column contains time stamps (e.g., '2023-12-31 14:30:00'), querying BETWEEN '2023-01-01' AND '2023-12-31' truncates the end date to '2023-12-31 00:00:00', excluding later times on that day.
Q4: Can I use multiple WHERE clauses in a single query?
No, a single SELECT statement can only have one WHERE clause. Combine multiple conditions using logical operators like AND and OR.
Q5: What is the cleanest way to search for multiple string patterns?
You can use regular expressions (REGEXP or RLIKE in MySQL/PostgreSQL) instead of multiple LIKE conditions connected with OR.
Q6: Does filtering using WHERE improve query performance?
Yes, filtering with indexed columns in the WHERE clause dramatically speeds up performance by scanning fewer rows.
Q7: Can I use aggregate functions inside a WHERE clause?
No, functions like SUM(), AVG(), or COUNT() cannot be placed in a WHERE clause. Use the HAVING clause instead.
Q8: What does WHERE 1=0 do?
It returns an empty result set while preserving the table structure/column names. It is often used to copy table structures.
Q9: How do I search for a literal % or _ character using LIKE?
You must escape the wildcard using an escape character, such as LIKE '%\%' ESCAPE '\'.
Q10: Is NOT (A AND B) the same as NOT A AND NOT B?
No, according to De Morgan's laws, NOT (A AND B) is equivalent to NOT A OR NOT B.
Q11: Can I filter data using subqueries in a WHERE clause?
Yes, subqueries are commonly used inside WHERE clauses with operators like IN, ANY, or ALL.
Q12: What happens if I perform IN (1, 2, NULL)?
Matching values (1 or 2) return TRUE. Non-matching rows evaluate to UNKNOWN and are excluded.
Q13: What is the alternative to LIKE for complex string pattern matching?
Regex operators such as REGEXP (MySQL), ~ (PostgreSQL), or PATINDEX (SQL Server).
Q14: How to filter data when joining two tables?
You can place conditions either in the ON clause of the JOIN or in the main WHERE clause.
Q15: Why should I avoid placing functions on columns in WHERE clauses?
Applying functions like WHERE YEAR(JoiningDate) = 2022 prevents the database from using indexes on that column (Non-SARGable query).

Recommended Further Reading

© 2026 SQL Tutorial Hub. All Rights Reserved. Production-Ready HTML Template for Blogger.

No comments:

Post a Comment

Followers