SQL Medium Flashcards: Intermediate SQL Practice for Interviews

Showing 50 of 50 flashcards

Difficulty: MEDIUM

Type: Other

Add a SalaryGrade column using CASE: High >100k - Mid 50-100k - Low <50k.

SELECT name - salary - CASE WHEN salary > 100000 THEN 'High' WHEN salary BETWEEN 50000 AND 100000 THEN 'Mid' ELSE 'Low' END AS SalaryGrade FROM Employee;

Difficulty: MEDIUM

Type: Other

Calculate running total of sales by date.

SELECT sale_date - amount - SUM(amount) OVER (ORDER BY sale_date) AS running_total FROM Sales;

Difficulty: MEDIUM

Type: Other

Correct CASE syntax in SELECT?

CASE WHEN condition THEN result ... ELSE result END

Difficulty: MEDIUM

Type: Other

Count items in each order using a subquery.

SELECT o.order_id - (SELECT COUNT(*) FROM OrderItems oi WHERE oi.order_id = o.order_id) AS item_count FROM Orders o;

Difficulty: MEDIUM

Type: Other

Difference between DELETE and TRUNCATE?

DELETE is row-by-row and can be rolled back. TRUNCATE is bulk and faster but irreversible.

Difficulty: MEDIUM

Type: Other

Difference between ROW_NUMBER() - RANK() - and DENSE_RANK()?

ROW_NUMBER() gives unique numbers. RANK() gives same rank to ties but skips ranks. DENSE_RANK() gives same rank to ties but doesn't skip.

Difficulty: MEDIUM

Type: Other

Difference between UNION and UNION ALL?

UNION removes duplicates; UNION ALL includes all rows including duplicates.

Difficulty: MEDIUM

Type: Other

Difference between clustered and non-clustered index?

Clustered index defines row order; only one allowed. Non-clustered is a separate structure and you can have many.

Difficulty: MEDIUM

Type: Other

Does the BETWEEN operator include boundary values?

Yes - BETWEEN is inclusive.

Difficulty: MEDIUM

Type: Other

Explain the difference between an INNER JOIN and a LEFT JOIN - and give an example scenario for each.

INNER JOIN returns only matching rows; LEFT JOIN returns all rows from the left table - even without a match.

Difficulty: MEDIUM

Type: Other

Find duplicate emails in Customers table.

SELECT email FROM Customers GROUP BY email HAVING COUNT(*) > 1;

Difficulty: MEDIUM

Type: Other

Find employees earning more than average in their department.

SELECT name FROM Employee e WHERE salary > (SELECT AVG(salary) FROM Employee WHERE department_id = e.department_id);

Difficulty: MEDIUM

Type: Other

Find highest paid employee in each department using window function.

WITH Ranked AS (SELECT * - ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rn FROM Employee) SELECT * FROM Ranked WHERE rn = 1;

Difficulty: MEDIUM

Type: Other

Given an Orders table with columns (order_id - customer_id - order_date - amount) - write a query to find the total order amount for each customer for each year.

SELECT customer_id - YEAR(order_date) AS order_year - SUM(amount) AS total_amount FROM Orders GROUP BY customer_id - YEAR(order_date);

Difficulty: MEDIUM

Type: Other

How to extract year from a date column?

Use YEAR(date_column) or EXTRACT(YEAR FROM date_column) depending on DBMS.

Difficulty: MEDIUM

Type: Other

How to find rows in one query not in another?

Use EXCEPT or MINUS or a LEFT JOIN with IS NULL.

Difficulty: MEDIUM

Type: Other

How to improve a slow SQL query?

Add proper indexes - avoid SELECT * - limit scanned data - use EXPLAIN to analyze performance.

Difficulty: MEDIUM

Type: Other

How to remove duplicates leaving only one?

Use ROW_NUMBER() OVER PARTITION BY columns - then delete where row > 1.

Difficulty: MEDIUM

Type: Other

How to replace NULL with a default value in SQL?

Use COALESCE(column - 'default') or IFNULL/ISNULL depending on DBMS.

Difficulty: MEDIUM

Type: Other

How to reuse a subquery result in multiple parts of a query?

Use a CTE with WITH clause. Define the subquery once and reference it multiple times.

Difficulty: MEDIUM

Type: Other

How would you delete all customers who have no orders?

DELETE FROM Customers WHERE NOT EXISTS (SELECT 1 FROM Orders WHERE Orders.customer_id = Customers.customer_id);

Difficulty: MEDIUM

Type: Other

How would you find all employees who do not belong to any department?

SELECT e.name FROM Employee e LEFT JOIN Department d ON e.dept_id = d.id WHERE d.id IS NULL;

Difficulty: MEDIUM

Type: Other

Insert resigned employees into Alumni table from Employees table.

INSERT INTO Alumni (id - name - dept_id - join_date - leave_date - status) SELECT id - name - dept_id - join_date - leave_date - status FROM Employees WHERE status = 'Resigned';

Difficulty: MEDIUM

Type: Other

Join Employee - Department - and Location to get employee name - dept - city.

SELECT e.name - d.dept_name - l.city FROM Employee e JOIN Department d ON e.dept_id = d.dept_id JOIN Location l ON d.location_id = l.location_id;

Difficulty: MEDIUM

Type: Other

LIKE wildcard characters and meaning?

'%' matches any number of characters - '_' matches one character.

Difficulty: MEDIUM

Type: Other

ROWNUM vs ROW_NUMBER() in Oracle?

ROWNUM is assigned before ORDER BY. ROW_NUMBER() is a window function with sorting and partitioning support.

Difficulty: MEDIUM

Type: Other

What does DROP TABLE do vs TRUNCATE?

DROP removes the table and its data. TRUNCATE removes data but keeps table structure.

Difficulty: MEDIUM

Type: Other

What is a CROSS JOIN and how many rows does it return?

A CROSS JOIN returns the Cartesian product: m * n rows for m and n rows in the two tables.

Difficulty: MEDIUM

Type: Other

What is a Common Table Expression (CTE)?

A CTE is a named temporary result set defined using WITH. It's used to simplify complex queries and improve readability.

Difficulty: MEDIUM

Type: Other

What is a FULL OUTER JOIN in SQL and how does it differ from other join types?

A FULL OUTER JOIN returns all rows from both tables - matching rows where possible and filling NULLs where there is no match.

Difficulty: MEDIUM

Type: Other

What is a composite index and when is it useful?

An index on multiple columns. Useful when queries filter on those columns together.

Difficulty: MEDIUM

Type: Other

What is a correlated subquery?

A correlated subquery uses values from the outer query and executes once for each row of the outer query.

Difficulty: MEDIUM

Type: Other

What is a recursive CTE and when is it used?

A recursive CTE is a CTE that references itself - used for hierarchical or recursive data.

Difficulty: MEDIUM

Type: Other

What is a self join in SQL - and when would you use one?

A self join is when a table is joined with itself. It's used when rows in a table are related to other rows in the same table.

Difficulty: MEDIUM

Type: Other

What is a window function?

A window function performs a calculation across rows related to the current row without collapsing the result set.

Difficulty: MEDIUM

Type: Other

What is an index and how does it improve performance?

An index is a structure that speeds up searches on a table. It reduces full table scans by allowing direct access to matching rows.

Difficulty: MEDIUM

Type: Other

What is the difference between COUNT(*) and COUNT(column_name) in SQL?

COUNT(*) counts all rows - including NULLs; COUNT(column_name) counts only non-NULL values.

Difficulty: MEDIUM

Type: Other

What is the difference between the WHERE and HAVING clauses in SQL?

WHERE filters rows before GROUP BY; HAVING filters after GROUP BY - often using aggregate functions.

Difficulty: MEDIUM

Type: Other

What is the difference between using the IN operator versus the EXISTS operator in a subquery - and when might you use one over the other?

IN checks for membership in a fixed list or subquery result. EXISTS checks for the presence of at least one row and is more efficient for correlated subqueries.

Difficulty: MEDIUM

Type: Other

What is the typical order of execution (logical query processing order) of clauses in a SELECT statement in SQL?

The order is: FROM (and JOINs) - then WHERE - then GROUP BY - then HAVING - then SELECT (the select-list) - and finally ORDER BY (and LIMIT/OFFSET).

Difficulty: MEDIUM

Type: Other

When does adding an index NOT improve performance?

When the column has low selectivity or on small tables.

Difficulty: MEDIUM

Type: Other

When to use subquery over join?

When filtering based on aggregated results or readability is better with subqueries.

Difficulty: MEDIUM

Type: Other

Which SQL operation returns common rows from two queries?

INTERSECT

Difficulty: MEDIUM

Type: Other

Why does column = NULL never work?

Because NULL represents unknown. Use IS NULL or IS NOT NULL.

Difficulty: MEDIUM

Type: Other

Write a SELECT SQL query to find all products in the 'Electronics' category with a price between 1000 and 5000 - and sort the results by price in descending order.

SELECT product_name - price FROM Products WHERE category = 'Electronics' AND price BETWEEN 1000 AND 5000 ORDER BY price DESC;

Difficulty: MEDIUM

Type: Other

Write a SQL query to list employees and their manager names using a self join.

SELECT e.name AS employee_name - m.name AS manager_name FROM Employee e LEFT JOIN Employee m ON e.manager_id = m.employee_id;

Difficulty: MEDIUM

Type: Other

Write a SQL statement to increase salary by 10% for employees earning less than the average salary.

UPDATE Employees SET salary = salary * 1.10 WHERE salary < (SELECT AVG(salary) FROM Employees);

Difficulty: MEDIUM

Type: Other

Write a query to get a list of all employees and their department names - including employees who are not assigned to any department.

SELECT e.name - d.department_name FROM Employee e LEFT JOIN Department d ON e.department_id = d.department_id;

Difficulty: MEDIUM

Type: Other

Write a query to retrieve all unique job titles from an Employee table - sorted alphabetically.

SELECT DISTINCT job_title FROM Employee ORDER BY job_title ASC;

Difficulty: MEDIUM

Type: Other

Write an SQL query to list each product category and its total sales from a Sales table - including only categories with total sales above 1 -000 -000. Order the results by total sales descending.

SELECT category - SUM(sales_amount) AS total_sales FROM Sales GROUP BY category HAVING SUM(sales_amount) > 1000000 ORDER BY total_sales DESC;

We use cookies to improve your experience. By clicking “Accept” you consent to the use of cookies. Read our Privacy Policy.