SQL Questions
SQL Questions
interview questions
* Produces a Cartesian
product of two tables.
* Functions like
`ROW_NUMBER()`, `RANK()`,
`LEAD()`, `LAG()` used over
partitions of data.
13. **What is the difference between
`UNION` and `UNION ALL`?**
```sql
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT
MAX(salary) FROM
employees);
```
17. **How do you retrieve duplicate
rows in a table?**
```sql
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;
```
```sql
SELECT * FROM table_name ORDER
BY column_name DESC LIMIT N;
```
19. **How to find rows that exist in one table but
not in another?**
```sql
SELECT *
FROM table1
WHERE NOT EXISTS (
SELECT 1 FROM table2 WHERE table1.id =
table2.id
);
```
20. **What is a Common Table Expression (CTE)?
**
* A temporary result set that can be referred
within `SELECT`, `INSERT`, `UPDATE`, or
`DELETE` statements.
```sql
WITH cte_name AS (
SELECT ...
)
SELECT * FROM cte_name;
```