Explore tens of thousands of sets crafted by our community.
Database Joins and Subqueries
15
Flashcards
0/15
EQUI JOIN Example
A join that retrieves records with equal values in the key columns defined with the ON clause. Example: SELECT * FROM table1 JOIN table2 ON table1.key = table2.key;
RIGHT JOIN Example
Used to return all records from the right table, and the matched records from the left table. Example: SELECT Orders.OrderID, Employees.LastName FROM Employees RIGHT JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID;
FULL OUTER JOIN Example
Used to return all records when there is a match in either left or right table. Example: SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
ALL Subquery Example
Compares a value to every value returned by a subquery. Example: SELECT ProductName FROM Products WHERE ProductPrice > ALL (SELECT ProductPrice FROM Products WHERE ProductPrice > 10);
Derived Table Subquery Example
A subquery in a FROM clause creating a temporary table used in the outer query. Example: SELECT AVG(Price) FROM (SELECT Price FROM Products WHERE SupplierID = 2) AS SupplierPrices;
Exists Subquery Example
Used to test for the existence of any record in a subquery. Example: SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.SupplierId AND Price < 20);
LEFT JOIN Example
Used to return all records from the left table, and the matched records from the right table. Example: SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
CROSS JOIN Example
Used to create a Cartesian product of rows from two or more tables. Example: SELECT Customers.CustomerName, Products.ProductName FROM Customers CROSS JOIN Products;
INNER JOIN Example
Used to select rows with matching values in both tables. Example: SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Correlated Subquery Example
A subquery that depends on the outer query for its values. Example: SELECT e1.EmployeeName FROM Employees e1 WHERE EXISTS (SELECT * FROM Employees e2 WHERE e1.ManagerID = e2.EmployeeID);
Uncorrelated Subquery Example
A subquery that can be run independently of the outer query. Example: SELECT * FROM Employees WHERE EmployeeID IN (SELECT ManagerID FROM Employees);
ANY/SOME Subquery Example
Compares a value to each value returned by a subquery. Example: SELECT ProductName FROM Products WHERE ProductID = ANY (SELECT ProductID FROM OrderDetails WHERE Quantity > 100);
NATURAL JOIN Example
Implicitly joins tables based on columns with the same names. Example: SELECT * FROM table1 NATURAL JOIN table2;
SELF JOIN Example
Used to join a table to itself. Example: SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City FROM Customers A INNER JOIN Customers B ON A.CustomerID <> B.CustomerID WHERE A.City = B.City;
Scalar Subquery Example
A subquery that returns exactly one row and one column. Example: SELECT (SELECT COUNT(*) FROM Orders) AS TotalOrders;
© Hypatia.Tech. 2024 All rights reserved.