A SQL query can execute without errors and still produce an unexpected result.
It might return the wrong employees, sort salaries incorrectly, or select the wrong three records.
The issue often lies in how you use "WHERE", "ORDER BY", logical operators, and row-limiting clauses.
Let's explore five practical SQL examples using a simple employee table.
Sample Data
Assume we have the following "employees" table:
ID| Name| Dept| Salary
101| Amit| IT| 60000
102| Priya| HR| 45000
103| Rahul| IT| 75000
104| Neha| Fin| 55000
105| Sneha| IT| 60000
- Filter Rows Using WHERE
Question: Find employees earning more than ₹50,000.
SELECT emp_id, name, salary
FROM employees
WHERE salary > 50000;
This query returns Amit, Rahul, Neha, and Sneha.
Common mistake: Using ">=" instead of ">" changes the result when an employee earns exactly ₹50,000.
- ">" means greater than.
- ">=" means greater than or equal to.
Always choose the comparison operator that matches the business requirement.
- Sort Salaries Correctly
Question: Display employees from the highest salary to the lowest.
SELECT name, salary
FROM employees
ORDER BY salary DESC;
Remember:
- "DESC" sorts from highest to lowest.
- "ASC" sorts from lowest to highest.
Interview tip: Without "ORDER BY", SQL does not guarantee the order of returned rows.
- Combine Filtering and Sorting
Question: Find IT employees earning more than ₹50,000 and display the highest salary first.
SELECT name, salary
FROM employees
WHERE dept = 'IT'
AND salary > 50000
ORDER BY salary DESC;
The query first identifies employees who satisfy both conditions and then sorts the qualifying rows by salary.
Expected result:
Name| Salary
Rahul| 75000
Amit| 60000
Sneha| 60000
Notice that Amit and Sneha have the same salary. Their relative order is not guaranteed because the query does not specify a secondary sort key.
This pattern is useful in employee reports, sales analysis, and dashboards.
- Avoid AND/OR Logic Errors
Question: Find employees who work in IT or earn more than ₹60,000, provided their salary is at least ₹50,000.
SELECT name, dept, salary
FROM employees
WHERE (dept = 'IT' OR salary > 60000)
AND salary >= 50000;
The parentheses make the intended logic explicit.
In SQL, "AND" has higher precedence than "OR". Without parentheses, a condition combining both operators can return unintended results.
Interview tip: When a requirement contains both "AND" and "OR", group the conditions carefully before writing the query.
- Find the Top 3 Highest-Paid Employees
Question: Return the three highest-paid employees.
SELECT emp_id, name, salary
FROM employees
ORDER BY salary DESC, emp_id ASC
LIMIT 3;
The query sorts by salary in descending order and uses "emp_id" as a secondary sort key.
Because Amit and Sneha both earn ₹60,000, the secondary sort makes their relative order predictable.
Important: "LIMIT" works in MySQL and PostgreSQL. Other database systems may use different row-limiting syntax.
Also, "LIMIT 3" returns at most three rows. If you need to include everyone tied at the third-highest salary, consider using a ranking function such as "RANK()".
One Small Change Can Affect Your Report
Compare these conditions:
WHERE salary > 50000
WHERE salary >= 50000
The difference is just one character, but the second condition also includes employees earning exactly ₹50,000.
In real reporting, small differences like this can affect totals, eligibility checks, and business decisions.
Continue Learning SQL
Want to practise more questions about filtering, sorting, and SQL interview preparation?
Explore the "SQL FAQ tutorial on restricting and sorting data" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/restricting-sorting-data-sql-faq.html) from Sankalan Data Tech for more topic-focused examples.
Final SQL Challenge
Suppose the "employees" table contains thousands of records.
How would you find the second-highest distinct salary without using "LIMIT" or "TOP"?
Try solving it with a subquery before looking up the answer. This is a useful exercise for SQL interviews.
Share your approach in the comments!
Top comments (0)