Exit Chapter 12 Β· πŸ”— Grouping Records, Joins in SQL β€” Quiz

Playing as a guest? to earn XP and track your progress.

Question 1 of 55 0 correct so far

πŸ”— Grouping Records, Joins in SQL Quiz

Q1.

Which category of SQL functions operates on a group of rows and returns a single summary value?

Explanation

Aggregate functions (COUNT, SUM, AVG, MAX, MIN) operate on a GROUP of rows and return ONE summary value for the entire group. Single-row functions process one row at a time.

Q2.

Which of the following is NOT an aggregate function in MySQL?

Explanation

UPPER() is a single-row string function β€” it processes one row at a time. SUM, COUNT, and AVG are all aggregate functions that work on groups of rows.

Q3.

What is the purpose of the GROUP BY clause in SQL?

Explanation

GROUP BY groups rows that have the same value in the specified column(s) into summary rows, allowing aggregate functions to produce one result per group.

Q4.

Based on the Employee table (Dept: SalesΓ—2, ITΓ—2, HRΓ—2), what does the following query return? SELECT Dept, COUNT(*) FROM Employee GROUP BY Dept;

Explanation

GROUP BY Dept creates three groups (Sales, IT, HR). COUNT(*) counts the rows in each group. Result: Sales→2, IT→2, HR→2 — three summary rows.

Q5.

Which query correctly displays the average salary for each department?

Explanation

To get the average salary per department, you need Dept in the SELECT list AND in the GROUP BY clause. AVG() is the correct aggregate function β€” AVERAGE() does not exist in MySQL.

Q6.

The Employee table has salaries: Sales(45000, NULL), IT(62000, 71000), HR(38000, 41000). What does SELECT Dept, AVG(Salary) FROM Employee GROUP BY Dept; return for Sales?

Explanation

AVG() ignores NULL values. For Sales: only 45000 is non-NULL. AVG = 45000 / 1 = 45000.00. The NULL salary (Anjali Gupta) is excluded from the calculation.

Q7.

Which of the following queries will produce an ERROR in MySQL (ONLY_FULL_GROUP_BY mode)?

Explanation

Name is neither in an aggregate function nor in the GROUP BY clause. MySQL cannot determine which employee's name to show for each department group. This violates the GROUP BY rule and causes an error.

Q8.

The Golden Rule of GROUP BY states that every column in the SELECT list that is NOT inside an aggregate function must:

Explanation

Any non-aggregated column in SELECT must be in GROUP BY. Otherwise MySQL cannot determine which value to display for the group (ambiguity error).

Q9.

Which query correctly groups employees by BOTH department AND city?

Explanation

Multiple columns in GROUP BY are separated by commas: GROUP BY Dept, City. This creates a separate group for each unique combination of (Dept, City). AND is not used in GROUP BY.

Q10.

Based on Employee table, how many rows does SELECT Dept, City, COUNT(*) FROM Employee GROUP BY Dept, City; return? (Sales: DelhiΓ—1, MumbaiΓ—1; IT: MumbaiΓ—1, BangaloreΓ—1; HR: DelhiΓ—2)

Explanation

GROUP BY Dept, City creates a group for each unique (Dept, City) combination: Sales-Delhi, Sales-Mumbai, IT-Mumbai, IT-Bangalore, HR-Delhi. That is 5 distinct combinations = 5 rows.

Q11.

What is the purpose of the HAVING clause in SQL?

Explanation

HAVING filters groups AFTER GROUP BY is applied. It is the group-level equivalent of WHERE. Unlike WHERE, HAVING can use aggregate functions in its condition.

Q12.

Which query correctly shows only departments where the average salary is more than 50000?

Explanation

HAVING must come AFTER GROUP BY. AVG cannot be used in WHERE. The correct order is: SELECT β†’ FROM β†’ WHERE β†’ GROUP BY β†’ HAVING β†’ ORDER BY.

Q13.

Based on the Employee table (IT avg=66500, Sales avg=45000, HR avg=39500), what is the output of: SELECT Dept, AVG(Salary) FROM Employee GROUP BY Dept HAVING AVG(Salary) > 50000;

Explanation

Only IT has AVG Salary (66500) above 50000. Sales (45000) and HR (39500) do not meet the HAVING condition, so they are filtered out.

Q14.

What is the key difference between WHERE and HAVING?

Explanation

WHERE filters individual rows BEFORE the GROUP BY operation. HAVING filters groups AFTER GROUP BY is applied. WHERE cannot use aggregate functions; HAVING can.

Q15.

Which of the following is INVALID SQL?

Explanation

AVG(Salary) cannot be used in a WHERE clause. Aggregate functions can only appear in SELECT, HAVING, or ORDER BY β€” not in WHERE. This query must use HAVING instead.

Q16.

A query uses both WHERE and HAVING. In what order do these clauses execute?

Explanation

Execution order: FROM β†’ WHERE (filter rows) β†’ GROUP BY (create groups) β†’ HAVING (filter groups) β†’ SELECT β†’ ORDER BY. WHERE always runs before GROUP BY; HAVING always runs after.

Q17.

What is the correct order of clauses in a complete SELECT statement?

Explanation

The mandatory clause order is: SELECT β†’ FROM β†’ WHERE β†’ GROUP BY β†’ HAVING β†’ ORDER BY. A helpful mnemonic: SF-WG-HO (Select From Where GroupBy Having OrderBy).

Q18.

Which query shows cities where the maximum salary exceeds 60000?

Explanation

MAX() is an aggregate function so it must be in HAVING (not WHERE). GROUP BY City creates one row per city. HAVING MAX(Salary) > 60000 then keeps only qualifying cities.

Q19.

What is a Cartesian Product in SQL?

Explanation

A Cartesian Product (Cross Join) pairs every row of the first table with every row of the second table. If Table A has m rows and Table B has n rows, the result has m Γ— n rows β€” no condition is used.

Q20.

Employee table has 6 rows and Department table has 4 rows. How many rows does SELECT * FROM Employee, Department; return?

Explanation

A Cartesian Product pairs every row of Employee (6) with every row of Department (4). Result = 6 Γ— 4 = 24 rows.

Q21.

Why is a Cartesian Product generally considered harmful in SQL queries?

Explanation

A Cartesian Product produces every possible combination of rows β€” most of which are meaningless (e.g., Priya paired with every department including the ones she doesn't belong to). It also causes severe performance problems with large tables.

Q22.

What is a Table Alias in SQL?

Explanation

A Table Alias gives a table a temporary, short name valid only within that query. It does NOT change the actual table name in the database. Example: FROM Employee E β€” 'E' is the alias.

Q23.

Which syntax correctly creates an alias 'E' for the Employee table?

Explanation

Table aliases can be created either with the AS keyword (FROM Employee AS E) or simply with a space (FROM Employee E). Both are valid MySQL syntax.

Q24.

In a join query, why are table aliases particularly useful?

Explanation

Table aliases shorten long table names and help resolve ambiguity when two tables have columns with the same name (e.g., both Employee and Department have DeptId). E.DeptId vs D.DeptId removes the confusion.

Q25.

What type of join combines rows from two tables based on an equality condition on a common column?

Explanation

An Equi-Join links two tables using an equality (=) condition, typically matching a Foreign Key in one table with the Primary Key in another (e.g., WHERE E.DeptId = D.DeptId).

Q26.

Which query correctly performs an Equi-Join between Employee (alias E) and Department (alias D) on DeptId?

Explanation

An Equi-Join requires a WHERE clause with an equality condition linking the two tables. Without WHERE E.DeptId = D.DeptId, it becomes a Cartesian Product. EQUI JOIN is not a valid MySQL keyword.

Q27.

Employee table has DeptId as a Foreign Key referencing Department(DeptId). Department D04 (Finance) has no employees. In an Equi-Join query, will Finance appear in the output?

Explanation

An Equi-Join (which is an Inner Join) only returns rows where matching values exist in BOTH tables. Since no employee has DeptId = D04, Finance has no matching employee row and does not appear in the output.

Q28.

Which of the following is the modern alternative syntax for an Equi-Join?

Explanation

The modern JOIN...ON syntax (FROM Employee JOIN Department ON Employee.DeptId = Department.DeptId) is equivalent to the traditional comma + WHERE syntax. Both produce identical results in MySQL.

Q29.

What is a Natural Join?

Explanation

Natural Join automatically identifies all columns that share the same name and data type in both tables and joins on those columns. No explicit join condition needs to be written.

Q30.

Employee and Department tables both have a column named DeptId. Which query performs a Natural Join?

Explanation

NATURAL JOIN is the correct keyword. MySQL automatically finds DeptId as the common column name and joins on it without needing a WHERE or ON condition.

Q31.

How does Natural Join handle duplicate columns (columns with the same name in both tables)?

Explanation

Natural Join eliminates duplicate columns from the result. If both tables have DeptId, the output contains DeptId only once. This is a key difference from Equi-Join, where DeptId appears twice (once from each table).

Q32.

What is a risk of using Natural Join compared to Equi-Join?

Explanation

Natural Join joins on ALL shared column names automatically. If two tables unexpectedly share multiple column names, the join might produce incorrect results. Equi-Join is safer because you explicitly control which column(s) to join on.

Q33.

In a join query, how do you add an additional filter condition besides the join condition?

Explanation

Additional search conditions are simply added to the WHERE clause using AND. Example: WHERE E.DeptId = D.DeptId AND E.City = 'Delhi'. The first condition joins the tables; the second filters the result.

Q34.

Which query displays the name, salary, and department name of employees working in Delhi?

Explanation

A join query needs BOTH the join condition (E.DeptId = D.DeptId) AND the filter condition (E.City = 'Delhi') connected with AND. Option A is missing the join condition (produces a Cartesian Product filtered by city). Option C cannot get DeptName without joining Department.

Q35.

Based on the Employee and Department tables, the query SELECT E.Name, D.DeptName FROM Employee E, Department D WHERE E.DeptId = D.DeptId AND E.Salary > 60000; returns which employees?

Explanation

Employees with Salary > 60000: Arjun Kumar (62000, IT) and Rohan Das (71000, IT). The join condition links them to their department names. Result: Arjun→IT, Rohan→IT.

Q36.

Which clause CANNOT use aggregate functions like AVG() or SUM()?

Explanation

Aggregate functions CANNOT be used in the WHERE clause. They can appear in SELECT, HAVING, and ORDER BY. WHERE filters individual rows before aggregation, so aggregate values are not yet computed at that stage.

Q37.

Which query finds departments with more than 1 employee?

Explanation

COUNT(*) > 1 is a group-level condition, so it goes in HAVING (not WHERE). GROUP BY Dept must come before HAVING. WHERE cannot use COUNT(). HAVING without GROUP BY would treat the whole table as one group.

Q38.

What is the output of the query: SELECT City, COUNT(*) AS Total FROM Employee GROUP BY City HAVING COUNT(*) > 1; (Delhi:3, Mumbai:2, Bangalore:1)

Explanation

HAVING COUNT(*) > 1 keeps only groups with more than 1 row. Delhi has 3 employees (>1 βœ…), Mumbai has 2 (>1 βœ…), Bangalore has 1 (not >1 ❌). So Delhi and Mumbai are returned.

Q39.

In a GROUP BY query, which combination is used to both group AND filter groups?

Explanation

GROUP BY creates the groups; HAVING filters those groups based on a condition (usually involving an aggregate function). This is the standard pairing for grouped queries with conditions.

Q40.

What does the following query do? SELECT Dept, COUNT(*) FROM Employee WHERE City = 'Delhi' GROUP BY Dept;

Explanation

Execution order: WHERE City='Delhi' runs first and removes Mumbai and Bangalore employees. The remaining Delhi employees are then grouped by Dept and counted. WHERE runs BEFORE GROUP BY.

Q41.

Equi-Join is also known as which type of join?

Explanation

Equi-Join is a type of Inner Join β€” it returns only rows where the join condition is satisfied in BOTH tables. Rows without a match in either table are excluded from the result.

Q42.

A student writes: SELECT Name, DeptName FROM Employee, Department; without any WHERE clause. What is the result?

Explanation

Without a WHERE condition linking the tables, this produces a Cartesian Product. Every employee is paired with every department regardless of actual department membership. For 6 employees and 4 departments: 24 rows.

Q43.

Which of the following correctly uses an alias to write a self-contained Equi-Join?

Explanation

Using aliases AS E and AS D, then qualifying columns as E.Name and D.DeptName with the join condition E.DeptId = D.DeptId is the correct and unambiguous syntax for an Equi-Join with table aliases.

Q44.

What is nested grouping in SQL?

Explanation

Nested grouping means using multiple columns in GROUP BY (e.g., GROUP BY Dept, City). This creates finer sub-groups β€” each unique combination of (Dept, City) forms its own group.

Q45.

Which query displays the total salary paid in each city, for cities where more than 2 employees work?

Explanation

COUNT(*) > 2 is a group condition β€” it goes in HAVING, not WHERE. GROUP BY comes before HAVING. Correct order: FROM β†’ GROUP BY β†’ HAVING. Option D has HAVING before GROUP BY which is invalid.

Q46.

What will SELECT MAX(Salary) FROM Employee GROUP BY Dept; return?

Explanation

GROUP BY Dept creates three groups. MAX(Salary) finds the highest salary within each group. Result: 3 rows β€” one maximum salary value per department.

Q47.

In the Equi-Join result of Employee and Department, how many rows are returned if every employee has a valid DeptId?

Explanation

In an Equi-Join (Inner Join), each Employee row matches exactly one Department row (since DeptId in Employee is a Foreign Key). So the result has the same number of rows as the Employee table β€” one row per employee.

Q48.

Which statement is TRUE about the difference between Equi-Join and Natural Join?

Explanation

Natural Join is convenient (no condition to write) but less precise β€” it joins on ALL shared column names which can sometimes produce unexpected results. Equi-Join gives the programmer explicit control over exactly which columns to join on.

Q49.

Which query shows each department name with the count of employees, but only for departments with at least 2 employees, sorted by count in descending order?

Explanation

This query needs: a join condition (WHERE E.DeptId = D.DeptId), grouping (GROUP BY D.DeptName), group filtering (HAVING COUNT(*) >= 2), and sorting (ORDER BY Total DESC). Option A has all in the correct order.

Q50.

A student wants to display each department name and its average salary. They write: SELECT DeptName, AVG(Salary) FROM Employee GROUP BY DeptName; β€” What is wrong with this query?

Explanation

DeptName is stored in the Department table, not in the Employee table. To display DeptName with salary data from Employee, you need an Equi-Join: FROM Employee E, Department D WHERE E.DeptId = D.DeptId GROUP BY D.DeptName.

Q51.

What does the following query display? SELECT Dept, MIN(Salary), MAX(Salary) FROM Employee GROUP BY Dept;

Explanation

GROUP BY Dept creates 3 groups (Sales, IT, HR). MIN(Salary) and MAX(Salary) are both computed for each group. Result: 3 rows, each showing a department with its min and max salary.

Q52.

How many columns does SELECT * FROM Employee NATURAL JOIN Department; return compared to SELECT * FROM Employee, Department WHERE Employee.DeptId = Department.DeptId;?

Explanation

Natural Join removes duplicate columns β€” DeptId (common to both tables) appears only once in the Natural Join result. The Equi-Join with * returns DeptId twice (once from each table). So Natural Join has one fewer column.

Q53.

Which query correctly displays the department name and total salary for departments where total salary exceeds 100000?

Explanation

The query needs: join condition in WHERE, GROUP BY for the department name, and HAVING for the aggregate condition (SUM > 100000). SUM cannot be used in WHERE. Option C is missing the join condition.

Q54.

In SQL, the execution order of clauses in a grouped query is:

Explanation

The logical execution order is: FROM (identify tables) β†’ WHERE (filter rows) β†’ GROUP BY (create groups) β†’ HAVING (filter groups) β†’ SELECT (compute output) β†’ ORDER BY (sort). This differs from the writing order.

Q55.

A query joins Employee and Department tables without any WHERE condition and returns 30 rows. If the Department table has 5 rows, how many rows does the Employee table have?

Explanation

Without a WHERE condition, the result is a Cartesian Product. If the result is 30 rows and Department has 5 rows, then Employee must have 30 Γ· 5 = 6 rows.