Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 129

MySQL Test 3 – Advanced SQL

In this lesson: Free advanced SQL test — joins, subqueries, views, transactions and case study — timer and answer review. Take it now.

MySQL Test 3 – Advanced SQL and Case Study (50 Marks)

This is an interactive MySQL/SQL test. It shows one question at a time with timer, next/previous buttons, question palette, result and answer review.

Case study tables (for questions marked [Case study])

customers (id INT PRIMARY KEY, name, city)

idnamecity
1AshaDelhi
2RaviPune
3MeenaDelhi
4JohnJaipur

orders (id INT PRIMARY KEY, customer_id INT REFERENCES customers(id), amount INT, order_date DATE)

idcustomer_idamountorder_date
10115002026-01-05
102112002026-01-20
10323002026-02-02
104315002026-02-10
10537002026-03-01

Every case-study answer was checked by running the query on a real MySQL-compatible server.

Question 1/5050:00

Question Palette

Green = answered, Purple = review, White = not answered.

Answer key with explanations

Attempt the test and submit it first, then use this key to revise. Each answer has a short explanation so you understand why it is correct.

Show all 50 answers
  1. Which join returns only rows that have matching values in both tables?

    Answer: B. INNER JOIN – Rows without a match in either table are dropped.

  2. Which join returns all rows of the left table and matching rows of the right table?

    Answer: A. LEFT JOIN – Unmatched right-side columns appear as NULL.

  3. A CROSS JOIN of a 4-row table with a 5-row table returns:

    Answer: D. 20 rows – A cross join is the Cartesian product: 4 × 5.

  4. Joining a table with itself (e.g. employee and manager in one table) is called:

    Answer: B. Self join – Use aliases: FROM emp e JOIN emp m ON e.manager_id = m.id.

  5. MySQL does not support FULL OUTER JOIN directly. It can be produced with:

    Answer: A. LEFT JOIN UNION RIGHT JOIN – UNION removes duplicates of the matched rows.

  6. UNION and UNION ALL differ because:

    Answer: B. UNION removes duplicate rows, UNION ALL keeps them – UNION ALL is faster when duplicates are acceptable.

  7. A subquery is:

    Answer: B. A query written inside another query – Example: SELECT name FROM students WHERE marks > (SELECT AVG(marks) FROM students);

  8. A correlated subquery:

    Answer: B. Refers to a column of the outer query and runs once per outer row – Example: find students scoring above their own class average.

  9. Which operator checks whether a subquery returns any row?

    Answer: D. EXISTS – WHERE EXISTS (SELECT 1 FROM orders o WHERE o.cust_id = c.id).

  10. marks > ALL (SELECT marks FROM students WHERE class = 9) means:

    Answer: C. Greater than the highest class-9 mark – > ANY means greater than at least one value (i.e. the minimum).

  11. Which window function gives 1, 2, 2, 4 for marks 95, 90, 90, 85?

    Answer: A. RANK() – RANK leaves gaps after ties; DENSE_RANK would give 1, 2, 2, 3.

  12. Which window function gives 1, 2, 2, 3 for marks 95, 90, 90, 85?

    Answer: A. DENSE_RANK() – DENSE_RANK does not skip numbers after ties (MySQL 8+).

  13. SUM(amount) OVER (ORDER BY order_date) calculates:

    Answer: B. A running total – Window functions keep every row while adding an aggregate.

  14. A CTE (Common Table Expression) starts with the keyword:

    Answer: D. WITH – WITH toppers AS (SELECT …) SELECT * FROM toppers; (MySQL 8+).

  15. Which statement shows how MySQL will execute a query (index usage etc.)?

    Answer: A. EXPLAIN SELECT … – EXPLAIN shows the access type, keys used and rows estimated.

  16. An index on (last_name, first_name) helps most for:

    Answer: A. WHERE last_name = 'Sharma' AND first_name = 'Ravi' – Composite indexes are used from the leftmost column (leftmost-prefix rule).

  17. A stored procedure is called using:

    Answer: A. CALL proc_name(); – Procedures can take IN, OUT and INOUT parameters.

  18. Why is DELIMITER $$ used before CREATE PROCEDURE in the MySQL client?

    Answer: B. So the semicolons inside the body do not end the statement early – Reset with DELIMITER ; afterwards.

  19. A trigger runs:

    Answer: B. Automatically before or after INSERT, UPDATE or DELETE on a table – Example: a BEFORE INSERT trigger that converts names to upper case.

  20. Inside an UPDATE trigger, the new value of a column is accessed as:

    Answer: B. NEW.column – OLD.column gives the value before the update.

  21. A SAVEPOINT allows you to:

    Answer: A. Roll back part of a transaction – ROLLBACK TO sp1; undoes changes made after the savepoint.

  22. Which isolation level is the InnoDB default?

    Answer: C. REPEATABLE READ – It prevents dirty and non-repeatable reads.

  23. Reading another transaction's uncommitted change is called a:

    Answer: A. Dirty read – Allowed only at READ UNCOMMITTED.

  24. Two transactions each waiting for a lock held by the other is a:

    Answer: D. Deadlock – InnoDB detects deadlocks and rolls back one transaction.

  25. Which tool creates a logical backup (SQL file) of a MySQL database?

    Answer: C. mysqldump – mysqldump -u root -p school > school.sql; restore with mysql school < school.sql.

  26. Which clause prevents SQL injection in application code?

    Answer: A. Prepared statements with parameters – Never build SQL by joining user input into the query string.

  27. CASE in SQL is used to:

    Answer: D. Return values based on conditions inside a query – CASE WHEN marks >= 90 THEN 'A1' WHEN marks >= 80 THEN 'A2' ELSE 'B' END.

  28. ON DELETE CASCADE on a foreign key means:

    Answer: A. Deleting a parent row automatically deletes its child rows – Use with care – one DELETE can remove many related rows.

  29. Which data type stores JSON documents with validation in MySQL 8?

    Answer: B. JSON – Use ->> or JSON_EXTRACT to read values.

  30. GROUP_CONCAT(name) in a GROUP BY query returns:

    Answer: D. All names of the group joined in one string – Default separator is a comma; SEPARATOR ' | ' changes it.

  31. [Case study] SELECT COUNT(*) FROM orders; returns:

    Answer: D. 5 – The orders table has 5 rows (ids 101–105).

  32. [Case study] SELECT SUM(amount) FROM orders WHERE customer_id = 1; returns:

    Answer: A. 1700 – Asha's orders: 500 + 1200 = 1700.

  33. [Case study] Which customer has no orders?

    Answer: C. John – Customer 4 (John) has no row in orders; find him with LEFT JOIN … WHERE o.id IS NULL.

  34. [Case study] SELECT COUNT(*) FROM customers c JOIN orders o ON c.id = o.customer_id; returns:

    Answer: A. 5 – Every order matches exactly one customer, so the inner join has 5 rows.

  35. [Case study] SELECT COUNT(*) FROM customers c LEFT JOIN orders o ON c.id = o.customer_id; returns:

    Answer: A. 6 – 5 matched rows + 1 row for John with NULL order columns.

  36. [Case study] SELECT city, SUM(amount) … GROUP BY city — the total for Delhi is:

    Answer: A. 3900 – Delhi customers Asha (1700) and Meena (2200): 1700 + 2200 = 3900.

  37. [Case study] Which customer has the highest total order amount?

    Answer: D. Meena – Totals: Asha 1700, Ravi 300, Meena 2200.

  38. [Case study] SELECT MAX(amount) FROM orders WHERE order_date < '2026-02-01'; returns:

    Answer: B. 1200 – January orders are 500 and 1200.

  39. [Case study] SELECT AVG(amount) FROM orders; returns:

    Answer: A. 840.0000 – Total 4200 ÷ 5 orders = 840 (MySQL shows 840.0000).

  40. [Case study] How many customers have total orders above 1000 (… GROUP BY customer_id HAVING SUM(amount) > 1000)?

    Answer: A. 2 – Asha (1700) and Meena (2200).

  41. [Case study] SELECT COUNT(DISTINCT customer_id) FROM orders; returns:

    Answer: D. 3 – Customers 1, 2 and 3 placed orders.

  42. [Case study] SELECT MONTH(order_date) m, COUNT(*) FROM orders GROUP BY m — the count for month 2 is:

    Answer: D. 2 – Orders 103 and 104 are in February.

  43. [Case study] Which query lists customers who placed at least one order?

    Answer: D. SELECT name FROM customers WHERE id IN (SELECT customer_id FROM orders); – The subquery returns 1, 2, 3, 3, 1 → Asha, Ravi, Meena.

  44. [Case study] SELECT name FROM customers WHERE city = 'Delhi' ORDER BY name DESC LIMIT 1; returns:

    Answer: A. Meena – Delhi names sorted Z–A: Meena, Asha.

  45. [Case study] For Asha, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) gives order 102 the number:

    Answer: A. 2 – Asha's orders by date: 101 (1st), 102 (2nd).

  46. [Case study] To stop deleting a customer who still has orders, the foreign key should use:

    Answer: D. ON DELETE RESTRICT (the default) – RESTRICT/NO ACTION blocks the delete with error 1451.

  47. [Case study] Which index would most speed up WHERE customer_id = ? on orders?

    Answer: D. An index on orders(customer_id) – InnoDB automatically creates this index when you define the foreign key.

  48. [Case study] SELECT c.name, o.amount FROM customers c JOIN orders o ON c.id = o.customer_id WHERE o.amount > (SELECT AVG(amount) FROM orders); returns how many rows?

    Answer: A. 2 – Average is 840; orders above it: 1200 (Asha) and 1500 (Meena).

  49. [Case study] SELECT COALESCE(SUM(o.amount), 0) FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE c.name = 'John'; returns:

    Answer: B. 0 – John has no orders, so SUM is NULL and COALESCE turns it into 0.

  50. [Case study] Which statement adds a 10% discount to orders above 1000?

    Answer: A. UPDATE orders SET amount = amount * 0.9 WHERE amount > 1000; – UPDATE with WHERE changes only orders 102 and 104.

🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicComplete MySQL MCQ Quiz – 150 Questions Your progress: 129 / 151 (85%) · Lesson 130 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free