Given two tables, Customers and Orders, which SQL query correctly returns all customers and their orders, including customers with no orders?
SELECT Customers.CustomerID, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Think about what a LEFT JOIN does compared to INNER JOIN.
A LEFT JOIN returns all rows from the left table (Customers) and matching rows from the right table (Orders). If there is no match, the right side columns are NULL.
Which statement best explains why foreign keys are important in relational databases?
Think about what happens if you try to link data that doesn't exist.
Foreign keys ensure that the value in one table matches a valid value in another, preventing invalid or orphaned data.
Which option contains a syntax error in the SQL JOIN statement?
SELECT e.Name, d.DepartmentName FROM Employees e INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID;
Check the JOIN syntax and how conditions are specified.
Option D uses WHERE instead of ON for the JOIN condition, which is invalid syntax for JOIN clauses.
You have two large tables, Orders and Customers. Which approach optimizes the query to find customers with orders in the last month?
Consider which method avoids unnecessary row combinations and improves performance.
Using WHERE EXISTS with a subquery efficiently checks for related rows without joining all data, improving performance on large tables.
Given these tables and query, why does the query return fewer rows than expected?
SELECT c.CustomerID, o.OrderID FROM Customers c LEFT JOIN Orders o ON c.CustomerID = o.CustomerID WHERE o.OrderDate > '2024-01-01';
Think about how WHERE affects rows with NULL values from LEFT JOIN.
Filtering on a column from the right table in WHERE removes rows where that column is NULL, negating the LEFT JOIN effect.