You have a table 'Customers' with a column 'LastPurchaseDate' that can contain NULL values. Write a query to retrieve all customers who have NOT made a purchase in the last 90 days.
Explanation:
This query correctly identifies customers who haven't made a purchase in the last 90 days by considering both NULL values (meaning they've never purchased) and dates older than 90 days ago. It uses IS NULL to check for null values and DATEADD function to subtract 90 days from the current date.
You have a table named 'Employees' with columns 'EmployeeID', 'FirstName', 'LastName', and 'Salary'. You need to categorize employees based on their salary. Write a SQL query using CASE statement to display 'High Earner' if the salary is above 50000, 'Average Earner' if the salary is between 30000 and 50000, and 'Low Earner' otherwise.
Explanation:
This query correctly uses the CASE statement to categorize employees based on the provided salary ranges. The BETWEEN operator is used effectively to define the range for 'Average Earners'.
What is the result of a CROSS JOIN between a table with 5 rows and a table with 3 rows?
Explanation:
A CROSS JOIN produces a Cartesian product of the two tables. This means every row in the first table is combined with every row in the second table, resulting in 5 * 3 = 15 rows.
You need to represent a hierarchical organization structure in a relational database table. Which approach is most suitable for querying and traversing this hierarchical data?
Explanation:
Recursive CTEs are ideal for representing and querying hierarchical data. They allow you to traverse the hierarchy recursively, retrieving data at different levels efficiently.
You are given a table 'Products' with columns 'ProductID', 'ProductName', and 'Price'. The Price column can contain NULL values for products not yet priced. Write a SQL query to retrieve all products, replacing NULL prices with the string 'Not Priced'.
Explanation:
This query utilizes the COALESCE function, which efficiently handles NULL values. It replaces NULL prices with 'Not Priced' while retaining the original prices for other products.
You're asked to retrieve the names of all employees who earn more than the average salary of their respective departments. This necessitates calculating the average salary per department and then comparing it to individual employee salaries. Which type of subquery would be most appropriate for this scenario?
Explanation:
A correlated subquery is executed once for each row in the outer query. In this case, for every employee, the subquery calculates the average salary of their department. This dynamic calculation, dependent on the outer query's row, makes correlated subqueries the right choice.
You have a table 'Orders' with order details and want to identify the first order placed by each customer based on the order date. Which combination of concepts would be most appropriate?
Explanation:
You can use ROW_NUMBER() to assign a unique rank to each order by customer based on order date. Encapsulating this logic in a CTE allows you to then filter for the first order (ROW_NUMBER() = 1) for each customer.
You need to find all customers in your 'Customers' table who haven't provided a phone number. The 'PhoneNumber' column can contain NULL values. Which query achieves this?
Explanation:
In SQL, NULL represents the absence of a value. To compare against NULL, you must use the 'IS NULL' operator. Using '=' with NULL will not yield the desired results.
Which of the following is a valid use case for a recursive CTE?
Explanation:
Recursive CTEs excel at traversing hierarchical or tree-like structures within data. They are particularly well-suited for scenarios like retrieving all employees and their managers in a hierarchy, where each level of management is connected.
Which window function would you use to assign a unique, sequential number to each row within a partition of a result set?
Explanation:
ROW_NUMBER() assigns a unique sequential integer to each row within the partition of a result set. It's used when you need a simple, unbroken sequence.
Which of the following SQL statements is used to definitively save changes made within a transaction?
Explanation:
In SQL, the COMMIT command is the instruction used to finalize changes made within a transaction. It signifies that all operations within the transaction were successful, and their effects should be permanently written to the database.
What is the purpose of the ROW_NUMBER() function in SQL?
Explanation:
The ROW_NUMBER() function in SQL is specifically designed to assign a unique sequential integer to each row within a partition of a result set. Partitions are determined by the PARTITION BY clause, and the numbering restarts for each partition.
What is the primary difference between a FULL OUTER JOIN and a UNION ALL operation in SQL?
Explanation:
The core distinction lies in how they combine data. FULL OUTER JOIN merges rows from tables based on a specified join condition, while UNION ALL directly appends all rows from the involved tables without considering any condition.
You have a table of orders and want to calculate the running total of order amounts for each customer, ordered by order date. Which SQL feature would be most efficient for this task?
Explanation:
Window functions with a suitable ORDER BY clause within the OVER clause are specifically designed to perform calculations across a set of rows related to the current row, such as running totals, making them the most efficient way to calculate running totals.
What SQL statement is used to apply the changes made within a transaction permanently to the database?
Explanation:
COMMIT is used to end a transaction and make all changes permanent in the database.