Answer: Aggregate Records Using MIN/MAX
Explore how to use SQL aggregate functions MIN and MAX to retrieve minimum and maximum values from a dataset. Understand different query techniques including subqueries, aliases, WHERE clauses, and common table expressions to find records like the second-highest salary. Gain practical skills to write precise SQL queries for interview questions.
Solution
The solution is given below:
Explanation
The explanation of the code solution is given below:
Line 2: The
SELECTquery selects the maximum value in theSalarycolumn using theMAX()function. We useASto set an alias for the column.Line 3: The
FROMclause specifies the table name asEmployees.Line 4: We selected the second highest salary using the subquery in the where clause to filter. The
NOT INoperator is a combination of two SQL keywords,NOTandIN. TheNOToperator allows the retrieval of the records that don’t match the condition and theINoperator is used to specify a list of items from which we decide on the values to retrieve.
Recall of relevant concepts
We have covered the following concepts in this question:
Selective columns
Aliases
Sorting the data
Limiting the records ... ...