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.
In SQL, a self-join is used to:
Explanation:
A self-join in SQL involves joining a table to itself, typically using aliases to differentiate between instances of the same table. This is useful for comparing rows within the same table based on specific criteria.
Which of these is NOT a common SQL performance tuning technique?
Explanation:
Using wildcard characters at the beginning of a search pattern often leads to full table scans, negating the benefits of indexes. It's more efficient to use other filtering methods or use wildcards at the end of the pattern if possible.
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.
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 is a CTE (Common Table Expression) in SQL?
Explanation:
A CTE is like a temporary view that exists only within the scope of a single query. It helps break down complex queries into more manageable parts.
What is a disadvantage of having too many indexes on a table?
Explanation:
While indexes speed up data retrieval, they also introduce overhead for data modification operations (insert, update, delete). Too many indexes can slow down these operations as the database needs to update all relevant indexes.
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.
Imagine you need to find pairs of employees who share the same job title without displaying the same employee twice (e.g., Employee A paired with Employee B is the same as Employee B with Employee A). Which SQL concept would be most efficient for this task?
Explanation:
A self-join allows you to join a table to itself, effectively treating it as two separate tables. This is ideal for comparing rows within the same table, such as finding employees with matching job titles without redundant pairings.
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.
Which ACID property ensures that any changes made within a transaction are applied to the database in a manner that prevents partial updates, preserving data consistency?
Explanation:
Atomicity in ACID properties mandates that a transaction is treated as a single, indivisible unit of work. Either all changes within the transaction are applied successfully, or none are, preventing partial updates and maintaining data integrity.
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 'Orders' with columns 'OrderID', 'CustomerID', and 'OrderDate'. You want to rank customers within each order date based on the order they placed (earliest to latest). Which window function should you use?
Explanation:
ROW_NUMBER() assigns a unique sequential number to each row within the partition defined by 'OrderDate', ordered chronologically by 'OrderID', thus ranking customers by their order placement within each date.
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.