FROM Employees
WHERE Department = 'IT'
OR Department = 'Finance';
Both departments will be included.
1️⃣9️⃣ IN
When checking multiple values, IN makes your query cleaner.
Instead of:
WHERE Department = 'IT'
OR Department = 'Finance'
OR Department = 'HR'
you can write:
WHERE Department IN ('IT', 'Finance', 'HR');
This is easier to read and maintain.
2️⃣0️⃣ NOT IN
You can exclude multiple values.
SELECT *
FROM Employees
WHERE Department NOT IN ('HR', 'Finance');
This returns employees who aren't in those departments.
2️⃣1️⃣ BETWEEN
BETWEEN checks whether a value falls within a range.
For example:
SELECT *
FROM Employees
WHERE Salary BETWEEN 50000 AND 80000;
This returns salaries within the specified range.
For numeric data, this is often useful for:
• Salary ranges
• Sales ranges
• Age ranges
• Scores
• Transaction values
2️⃣2️⃣ LIKE
LIKE is used for pattern matching.
Suppose you want employees whose names start with J.
SELECT *
FROM Employees
WHERE Name LIKE 'J%';
% means:
Any number of characters.
So this could match:
• John
• James
• Jennifer
2️⃣3️⃣ LIKE with Wildcards
•
Starts with J
LIKE 'J%'
•
Ends with n
LIKE '%n'
•
Contains "oh"
LIKE '%oh%'
Wildcards are extremely useful when searching text data.
2️⃣4️⃣ DISTINCT
DISTINCT removes duplicate values from the result.
Suppose your employee table contains:
• IT
• HR
• IT
• Finance
• HR
• IT
Use:
SELECT DISTINCT Department
FROM Employees;
Result:
IT
HR
Finance
This is useful for discovering categories in a dataset.
2️⃣5️⃣ ORDER BY
ORDER BY sorts your results.
Suppose you want employees with the highest salary first.
SELECT *
FROM Employees
ORDER BY Salary DESC;
DESC means:
Descending
Highest → Lowest
2️⃣6️⃣ ASC
ASC means ascending.
SELECT *
FROM Employees
ORDER BY Salary ASC;
Lowest → Highest
Ascending is generally the default sort direction.
2️⃣7️⃣ LIMIT / TOP
The syntax depends on the database system.
In systems such as PostgreSQL and MySQL:
SELECT *
FROM Employees
ORDER BY Salary DESC
LIMIT 5;
This returns the top 5 employees by salary.
In SQL Server, you would commonly use:
SELECT TOP 5 *
FROM Employees
ORDER BY Salary DESC;
This is an important point:
SQL is a language, but different database systems have slightly different syntax.
2️⃣8️⃣ Aliases
Aliases give columns or tables temporary names within a query.
For example:
SELECT
Name AS Employee_Name,
Salary AS Annual_Salary
FROM Employees;
The result displays:
Employee_Name Annual_Salary
John 75,000
Sarah 60,000
Aliases make results easier to understand.
2️⃣9️⃣ SQL Comments
You can add comments to explain your queries.
For example:
-- Get employees earning more than 70,000
SELECT Name, Salary
FROM Employees
WHERE Salary > 70000;
Comments don't affect the query result.
They're useful when queries become complex.
🧪 Practical Interview Challenge
Suppose you have:
Employees
ID Name Department Salary
101 John IT 75,000
102 Sarah HR 60,000
103 Mike Finance 82,000
104 David IT 90,000
105 Alice HR 65,000
Q1. Retrieve all employees.
SELECT *
FROM Employees;
Q2. Retrieve only names and salaries.
SELECT Name, Salary
FROM Employees;
Q3. Find employees earning more than ₹70,000.
SELECT *
FROM Employees
WHERE Salary > 70000;
Q4. Find IT employees.
SELECT *
FROM Employees
WHERE Department = 'IT';
Q5. Find IT or Finance employees.
SELECT *
FROM Employees
WHERE Department IN ('IT', 'Finance');
Q6. Sort employees by salary from highest to lowest.
SELECT *
FROM Employees
ORDER BY Salary DESC;
Q7. Find the top 3 highest-paid employees.
PostgreSQL/MySQL:
SELECT *
FROM Employees
ORDER BY Salary DESC
LIMIT 3;
SQL Server:
SELECT TOP 3 *
FROM Employees
ORDER BY Salary DESC;
Q8. List unique departments.
SELECT DISTINCT Department
FROM Employees;
🏆 Double Tap ❤️ For More
