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 |
|---|---|---|---|---|---|---|
| 1 | Rahul | Sharma | 15 | 10 | Delhi | 85.5 |
| 2 | Priya | Verma | 14 | 9 | Mumbai | 92.0 |
| 3 | Amit | Patel | 16 | 10 | Ahmedabad | NULL |
| 4 | Neha | Singh | 15 | 10 | Delhi | 68.0 |
| 5 | Rohan | Gupta | 14 | 9 | Bangalore | 74.5 |
| 6 | Ananya | Das | 16 | 11 | Kolkata | 95.0 |
Table 2: Employees
| EmpID | EmpName | Department | Salary | JoiningDate | ManagerID |
|---|---|---|---|---|---|
| 101 | Aarav Mehta | IT | 75000 | 2021-03-15 | NULL |
| 102 | Sanya Malhotra | HR | 50000 | 2022-06-01 | 101 |
| 103 | Vikram Rathore | IT | 85000 | 2020-01-10 | 101 |
| 104 | Kavita Roy | Finance | 60000 | 2023-08-20 | 102 |
| 105 | Rajesh Kumar | Sales | 45000 | 2022-11-12 | 102 |
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 |
|---|---|---|---|---|---|---|
| 1 | Rahul | Sharma | 15 | 10 | Delhi | 85.5 |
| 3 | Amit | Patel | 16 | 10 | Ahmedabad | NULL |
| 4 | Neha | Singh | 15 | 10 | Delhi | 68.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 |
|---|---|---|---|---|---|---|
| 1 | Rahul | Sharma | 15 | 10 | Delhi | 85.5 |
| 4 | Neha | Singh | 15 | 10 | Delhi | 68.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 |
|---|---|---|---|---|---|---|
| 1 | Rahul | Sharma | 15 | 10 | Delhi | 85.5 |
| 2 | Priya | Verma | 14 | 9 | Mumbai | 92.0 |
| 4 | Neha | Singh | 15 | 10 | Delhi | 68.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 |
|---|---|---|---|---|---|---|
| 3 | Amit | Patel | 16 | 10 | Ahmedabad | NULL |
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 |
|---|---|---|
WHERE | Filter individual rows based on condition | WHERE Age > 18 |
AND | Combine multiple conditions; all must be TRUE | WHERE Age > 18 AND Status = 'Active' |
OR | Combine conditions; at least one must be TRUE | WHERE City = 'Delhi' OR City = 'Noida' |
IN | Match any value in a list | WHERE Role IN ('Admin', 'Teacher') |
BETWEEN | Filter values within inclusive range | WHERE Salary BETWEEN 40000 AND 90000 |
LIKE | Pattern matching with % and _ wildcards | WHERE Name LIKE 'J%' |
IS NULL | Check for missing/unassigned values | WHERE ManagerID IS NULL |
12. 25 Beginner SQL Interview Questions & Answers
-
What is the primary purpose of the WHERE clause?
Answer: It filters rows returned by a query based on specified conditional criteria. -
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. -
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. -
How does SQL handle comparison with NULL values?
Answer: Standard comparisons (=, !=) return UNKNOWN. You must useIS NULLorIS NOT NULL. -
Is the BETWEEN operator inclusive or exclusive?
Answer: It is inclusive. Both boundary values are included in the result. - What does the % wildcard represent in a LIKE operator query?
Answer: Zero, one, or multiple characters. - What does the _ (underscore) wildcard represent?
Answer: Exactly one single character. - Write a query to find names ending with 'son'.
Answer:SELECT * FROM Users WHERE Name LIKE '%son'; - 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. - How do you negate a BETWEEN condition?
Answer: By usingNOT BETWEEN. - What is the difference between WHERE and HAVING?
Answer: WHERE filters individual rows before grouping; HAVING filters aggregated groups. - Does SQL LIKE matching case-sensitive?
Answer: It depends on database collation (e.g., MySQL default is case-insensitive, PostgreSQL LIKE is case-sensitive). - How to test if a string is empty vs NULL?
Answer: UseWHERE col = ''for empty strings andWHERE col IS NULLfor NULL values. - Can we use multiple AND and OR conditions in one query?
Answer: Yes, using parentheses to control evaluation order. - What happens if both sides of an AND condition are NULL?
Answer: The expression evaluates to NULL (UNKNOWN). - What query returns employees who do NOT have a manager assigned?
Answer:SELECT * FROM Employees WHERE ManagerID IS NULL; - How do you perform wildcards literal search if text has '%'?
Answer: Use an ESCAPE character, e.g.,LIKE '10\%' ESCAPE '\'. - What operator is used to search for values within a set returned by a subquery?
Answer: TheINorEXISTSoperator. - Is
WHERE 1=1valid SQL syntax?
Answer: Yes, it always evaluates to TRUE and is often used in dynamic query builders. - Which operator is best suited for date range queries?
Answer: TheBETWEENoperator or logical operators (>= AND <=). - What is the evaluation order of NOT, AND, and OR?
Answer: NOT has the highest precedence, followed by AND, then OR. - Can WHERE clause be used with UPDATE statements?
Answer: Yes, to filter which rows get updated. Without WHERE, all rows are updated! - Can WHERE clause be used with DELETE statements?
Answer: Yes, to specify which records to delete. - How to select records where a column value is NOT IN a list?
Answer: UseWHERE column NOT IN (val1, val2). - 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 (เคเคธाเคจ เคธ्เคคเคฐ)
- Write a SQL query to select all employees from the 'IT' department.
- Find all students who are 15 years old.
- Select all employees whose salary is greater than 60,000.
- List all students living in 'Mumbai'.
- Find all employees hired after '2022-01-01'.
- List all students whose marks are less than 75.
- Find employees with EmpID equal to 103.
- Select all students in Grade 9.
Medium Level (เคฎเคง्เคฏเคฎ เคธ्เคคเคฐ)
- Select students living in 'Delhi' AND having age equal to 15.
- Find employees working in 'IT' OR 'Finance' departments.
- Retrieve all students whose age is in the list (14, 16).
- Find all employees earning between 50,000 and 80,000 using BETWEEN.
- Select students whose FirstName starts with 'R'.
- Find all employees whose name ends with 'a'.
- Select all students whose marks value is currently missing (NULL).
- Retrieve employees who do NOT work in HR department.
- Select all students whose city is NOT IN ('Delhi', 'Mumbai').
Challenge Level (เคเค िเคจ เคธ्เคคเคฐ)
- Find IT employees earning over 70,000 OR HR employees earning over 45,000.
- Select students whose second letter of FirstName is 'a'.
- Write a query using BETWEEN to select students aged 14 to 16 excluding those from 'Delhi'.
- Retrieve all employees whose JoiningDate is in year 2022 using LIKE.
- Find students whose Marks are NOT NULL and greater than 80.
- Write a query combining WHERE, IN, and LIKE to find students from 'Delhi' or 'Mumbai' whose name contains 'a'.
- Select employees whose ManagerID is NOT NULL and Department = 'IT'.
- 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).

No comments:
Post a Comment