Views & Query Design Questions with Answers

31 practice questions on Views & Query Design (SQL) with answers and step-by-step solutions. Try 10 samples below, then practise all 31 free in the AptiNest app.

Sample questions

Question 1Easy

What is a view in SQL?

  1. A stored query that can be used like a virtual table
  2. A temporary table that is dropped at session end
  3. An index on several columns
  4. A physical copy of a table refreshed nightly

Answer: A. A stored query that can be used like a virtual table

Explanation

A (non-materialised) view stores only its SELECT; each query against it runs that SELECT on the current base data.

Step-by-step solution

  1. A view is a named SELECT stored in the database.
  2. Queries against the view run that SELECT on the current data.
  3. So a view works like a virtual table (option A).

Why the other options are wrong

  • B. A temporary table stores its own data; a view stores only the query.
  • C. An index speeds up lookups but is not a stored query.
  • D. A physical copy refreshed nightly describes a materialized view, not a normal view.

Tip: A normal view stores the query, not the data.

Question 2Easy

What does the final SELECT return?

items
id | name | cat  | price
1  | Rice | Food | 60
2  | Soap | Home | 40
3  | Tea  | Food | 120
4  | Mop  | Home | 250
5  | Oil  | Food | 180
CREATE VIEW food AS SELECT name, price FROM items WHERE cat = 'Food';
SELECT COUNT(*), MAX(price) FROM food;
  1. 5, 250
  2. 3, 120
  3. 2, 180
  4. 3, 180

Answer: D. 3, 180

Explanation

The view exposes the three Food items: Rice, Tea and Oil, with a top price of 180.

Step-by-step solution

  1. The view keeps only Food rows: Rice 60, Tea 120, Oil 180.
  2. COUNT(*) of the view is 3.
  3. MAX(price) is 180, so the result is 3, 180 (option D).

Why the other options are wrong

  • A. 5 counts all items, but the view filters to Food; 250 is Mop, which is Home.
  • B. 120 is Tea's price; the maximum Food price is Oil's 180.
  • C. The view has three Food rows, not two.

Tip: Check the view's WHERE before counting; a view's aggregates run on its filtered rows only.

Question 3Easy

How many rows does this query return?

matches
mid | home   | away   | hg   | ag
1   | Lions  | Tigers | 2    | 1
2   | Bears  | Lions  | 0    | 0
3   | Tigers | Bears  | 3    | 2
4   | Lions  | Bears  | 1    | 3
5   | Tigers | Lions  | NULL | NULL
SELECT * FROM matches WHERE hg + ag > 2;
  1. 4
  2. 5
  3. 2
  4. 3

Answer: D. 3

Explanation

Goal totals are 3, 0, 5, 4 and NULL; three exceed 2. NULL + NULL is NULL, so match 5 is excluded.

Step-by-step solution

  1. Totals hg + ag: match 1 is 3, match 2 is 0, match 3 is 5, match 4 is 4, match 5 is NULL.
  2. Totals above 2 are matches 1, 3 and 4.
  3. NULL + NULL is NULL, so match 5 is excluded. Count 3 (option D).

Why the other options are wrong

  • A. Four would need match 5 to pass, but NULL is never greater than 2.
  • B. Five counts every match, but matches 2 and 5 fail the test.
  • C. Two misses match 4, whose total is 4.

Tip: Arithmetic with NULL gives NULL, and NULL fails any comparison in WHERE.

Question 4Easy

Can a view be created on top of another view?

  1. Only if both views are materialized
  2. Only in the same session
  3. No, a view may only reference base tables
  4. Yes, views can be nested

Answer: D. Yes, views can be nested

Explanation

A view is a named query and may reference other views; the database expands them when the outer query runs.

Step-by-step solution

  1. A view is a named query and can refer to other views.
  2. The database expands the nested views when the outer query runs.
  3. So views can be nested (option D).

Why the other options are wrong

  • A. Materialization is not required for a view to use another view.
  • B. Views are stored in the database and are not tied to a session.
  • C. A view can reference other views as well as base tables.

Tip: Nested views work, but deep nesting can make queries hard to tune.

Question 5Medium

Which query design returns customers with no orders efficiently and correctly even if orders.customer_id contains NULLs?

  1. SELECT * FROM customers WHERE id <> ALL (SELECT id FROM orders);
  2. SELECT c.* FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
  3. SELECT c.* FROM customers c JOIN orders o ON o.customer_id <> c.id;
  4. SELECT * FROM customers WHERE id NOT IN (SELECT customer_id FROM orders);

Answer: B. SELECT c.* FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);

Explanation

NOT EXISTS is NULL-safe; NOT IN and <> ALL return nothing once the subquery has a NULL, and the <> join returns almost every pair.

Step-by-step solution

  1. NOT EXISTS checks for a matching order per customer and ignores NULL customer_id values.
  2. So customers with no orders are returned correctly even when the orders table has NULLs.
  3. That is option B.

Why the other options are wrong

  • A. <> ALL behaves like NOT IN, so one NULL in the list makes it return no rows; it also compares with orders.id.
  • C. The join returns pairs of customers and orders that differ, so it returns almost every pair.
  • D. NOT IN returns no rows once the subquery contains a NULL.

Tip: Use NOT EXISTS for 'no matching row' checks; it is safe with NULLs.

Question 6Medium

What does this query return?

matches
mid | home   | away   | hg   | ag
1   | Lions  | Tigers | 2    | 1
2   | Bears  | Lions  | 0    | 0
3   | Tigers | Bears  | 3    | 2
4   | Lions  | Bears  | 1    | 3
5   | Tigers | Lions  | NULL | NULL
SELECT home AS team, CASE WHEN hg > ag THEN 3 WHEN hg = ag THEN 1 ELSE 0 END AS pts FROM matches WHERE hg IS NOT NULL
UNION ALL
SELECT away, CASE WHEN ag > hg THEN 3 WHEN ag = hg THEN 1 ELSE 0 END FROM matches WHERE hg IS NOT NULL
ORDER BY 1, 2;
  1. 4 rows: one per played match
  2. 3 rows: one per team
  3. 8 rows: each team’s points per played match
  4. 10 rows: including the unplayed match

Answer: C. 8 rows: each team’s points per played match

Explanation

UNION ALL stacks a home row and an away row for each of the 4 played matches (8 rows); UNION would wrongly merge identical (team, pts) pairs.

Step-by-step solution

  1. WHERE hg IS NOT NULL drops match 5, which is not played.
  2. Each of the 4 played matches gives one home row and one away row.
  3. UNION ALL keeps all of them: 4 × 2 = 8 rows (option C).

Why the other options are wrong

  • A. There are 8 rows, not 4, because each match gives a home row and an away row.
  • B. Three is the number of teams, but each played match gives two rows, one per side.
  • D. Match 5 is excluded by the WHERE, so 10 is wrong; 10 would count the unplayed match too.

Tip: UNION ALL of a home and away row per match gives 2 rows per match; UNION would merge identical (team, points) rows.

Question 7Medium

What does the final SELECT return?

cities
id | name     | state
1  | Mysuru   | KA
2  | Hubballi | KA
3  | Nashik   | MH
4  | Nagpur   | MH
5  | Kochi    | KL
CREATE VIEW state_count AS
  SELECT state, COUNT(*) AS n FROM cities GROUP BY state;
SELECT state FROM state_count WHERE n = (SELECT MAX(n) FROM state_count) ORDER BY state;
  1. KA, MH
  2. MH
  3. KA
  4. KA, KL, MH

Answer: A. KA, MH

Explanation

KA and MH each have 2 cities, the maximum; KL has 1.

Step-by-step solution

  1. State_count gives KA 2 cities, MH 2, KL 1.
  2. The maximum count is 2.
  3. States with 2 are KA and MH, sorted (option A).

Why the other options are wrong

  • B. KA also has 2 cities, so it must be returned.
  • C. MH also has 2 cities, so it must be returned.
  • D. KL has 1 city, which is not the maximum.

Tip: A view of grouped counts can be filtered on the max of its own column, using a subquery.

Question 8Medium

What does this query return?

items
id | name | cat  | price
1  | Rice | Food | 60
2  | Soap | Home | 40
3  | Tea  | Food | 120
4  | Mop  | Home | 250
5  | Oil  | Food | 180
SELECT cat,
       SUM(CASE WHEN price >= 100 THEN 1 ELSE 0 END) AS costly,
       COUNT(*) AS total
FROM items
GROUP BY cat
ORDER BY cat;
  1. Food 2 3, Home 2 2
  2. Food 3 3, Home 1 2
  3. Food 2 3, Home 1 2
  4. Food 1 3, Home 1 2

Answer: C. Food 2 3, Home 1 2

Explanation

Conditional aggregation pivots counts: Food has Tea and Oil at 100+, Home has Mop.

Step-by-step solution

  1. Food: Rice 60 is below 100 (no), Tea 120 (yes), Oil 180 (yes). Costly 2, total 3.
  2. Home: Soap 40 (no), Mop 250 (yes). Costly 1, total 2.
  3. Output by category: Food 2 3, Home 1 2 (option C).

Why the other options are wrong

  • A. Soap at 40 is below 100, so Home has 1 costly item, not 2.
  • B. Rice at 60 is below 100, so Food has 2 costly items, not 3.
  • D. Tea (120) and Oil (180) are both 100 or more, so Food has 2 costly items, not 1.

Tip: Conditional counting: SUM(CASE WHEN condition THEN 1 ELSE 0 END) counts only matching rows.

Question 9Hard

A report needs, for each department, the employee with the highest salary, returning all employees if there is a tie. Which approach is correct?

  1. Use RANK() OVER (PARTITION BY dept ORDER BY salary DESC) in a subquery and keep rank 1
  2. ORDER BY salary DESC LIMIT 1
  3. Use ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) and keep row 1
  4. SELECT dept, name, MAX(salary) FROM emp GROUP BY dept

Answer: A. Use RANK() OVER (PARTITION BY dept ORDER BY salary DESC) in a subquery and keep rank 1

Explanation

RANK keeps all ties at rank 1. ROW_NUMBER picks one arbitrarily, the GROUP BY version is invalid in standard SQL, and LIMIT 1 ignores departments.

Step-by-step solution

  1. We need all top earners per department, ties included.
  2. RANK() gives tied rows the same rank 1.
  3. Keeping rank 1 in a subquery returns them all.

Why the other options are wrong

  • B. LIMIT 1 ignores departments and ties.
  • C. ROW_NUMBER gives one row only.
  • D. Non-aggregated name is not valid with GROUP BY.

Tip: Ties -> RANK.

Question 10Hard

What does WITH CHECK OPTION do on CREATE VIEW food AS SELECT * FROM items WHERE cat = 'Food' WITH CHECK OPTION (standard SQL / MySQL)?

  1. It rejects inserts or updates through the view that would produce rows not satisfying cat = 'Food'
  2. It validates the view definition when it is created
  3. It refreshes the view automatically
  4. It makes the view read-only

Answer: A. It rejects inserts or updates through the view that would produce rows not satisfying cat = 'Food'

Explanation

CHECK OPTION stops rows from "disappearing" from the view after modification by enforcing its WHERE clause on writes.

Step-by-step solution

  1. WITH CHECK OPTION applies the view's WHERE to inserts and updates through the view.
  2. A write that would produce a row outside cat = 'Food' is rejected.
  3. So option A is correct.

Why the other options are wrong

  • B. CHECK OPTION is about writes, not validation of the view definition.
  • C. Views are not refreshed by this clause; they always run their query.
  • D. The view stays writable; the clause only limits which rows can be written.

Tip: Use WITH CHECK OPTION when a view should not let users write rows they cannot see.

Practise all 31 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all SQL topics · All topics