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.
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 large table with millions of records. You frequently query the table based on a specific column 'customer_city'. What is the most effective way to improve the performance of these queries?
Explanation:
Adding an index to the frequently queried column 'customer_city' will significantly speed up data retrieval by allowing the database to quickly locate the relevant rows without scanning the entire table.
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.
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.
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.
What is the primary purpose of using a Common Table Expression (CTE) in SQL?
Explanation:
CTEs provide a way to organize and break down complex queries into smaller, more manageable parts. They are not persistent like views and are primarily used for readability and modularity within a single query.
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 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 are using a correlated subquery to compare values in each row of a table to aggregated data from another table. What is a potential performance concern with this approach?
Explanation:
While correlated subqueries provide an elegant solution in some cases, their nested nature can lead to performance bottlenecks if not used carefully.
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 named 'Employees' with columns 'EmployeeID', 'Department', and 'Salary'. Write a SQL query to fetch the top 3 highest-paid employees from each department, along with their rank within the department.
Explanation:
This query correctly utilizes a CTE and the ROW_NUMBER() function to first rank employees within each department based on salary. Then, it filters the results to include only the top 3 ranked employees from each department.
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 these tools can be used to analyze query performance in SQL?
Explanation:
A query execution plan is a visual representation of the steps a database takes to execute a query. Analyzing this plan can help identify bottlenecks and optimize query performance.
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.