Understanding Subqueries
In MySQL, what is the primary purpose of using a subquery within a SELECT statement?
- To encapsulate a query result that can be used by the outer query
- To speed up the database server's boot process
- To define a table's primary key
- To convert data types within a column
- To alter the structure of a database
Difference Between JOIN Types
Which JOIN type should you use if you want to return only the rows that have matching values in both tables?
- INNER JOIN
- OUTER JION
- LEFT OUTER JOIN
- RIGHT JOIN
- CROSS JOIN
Understanding GROUP BY with Aggregations
When calculating average salary for each year, which SQL clause ensures salaries are averaged per year separately?
- GROUP BY
- ORDER BY
- HAVING
- DISTINCT
- LIMIT
Using CASE WHEN in SELECT
In the provided query, how does the CASE WHEN statement help extract salaries for fielders and pitchers?
- It conditionally assigns average salary values based on the player's role
- It sorts players alphabetically by role
- It filters out teams with losing records
- It joins two different databases
- It multiplies salary values by two
Understanding UNION ALL
Why does the query use UNION ALL between the subqueries for fielders and pitchers?
- To combine all results from both roles without removing duplicate records
- To sort the results in alphabetical order
- To filter rows based on salary amount
- To create a new table in the database
- To convert rows into columns
Identifying the Role with the Highest Average Salary
Suppose your query finds that in 1985, fielders have a higher average salary than pitchers. What SQL function helps you retrieve the highest value per role and year?
- MAX
- MIN
- SUM
- COUNT
- AVG
Understanding Common Table Expressions (CTEs)
In the Teams ranking question, why is a WITH clause (CTE) such as TeamRank used before the final SELECT?
- To temporarily store ranked team data for reuse in the next query
- To limit the number of rows in the output
- To physically rearrange the data on disk
- To define new columns in the original table
- To encrypt the data in the table
Understanding RANK() Window Function
What is the purpose of RANK() OVER (PARTITION BY yearID ORDER BY W DESC) in the context of the Teams table?
- It assigns a ranking to teams based on number of wins within each year
- It deletes all teams with the highest wins
- It calculates the average wins for all years
- It sums the wins of the top team
- It filters teams by teamID
Using CASE to Label Results
Why is the CASE statement used in the SELECT when showing 'First Place' or 'Last Place' teams?
- To label teams based on their calculated rank values
- To permanently rename every row in the Teams table
- To skip rows with missing values
- To convert numbers to strings
- To join multiple tables together
Filtering Results for Specific Ranks
In the final query for ranking teams, which condition ensures only teams in first and last places appear in the results?
- WHERE rank_min = 1 OR rank_max = 1
- HAVING salary u003E 100000
- LIMIT 2
- WHERE teamID IS NOT NULL
- GROUP BY place