SELECT Products.prod_id, Products.prod_name FROM Products;
SELECT P.prod_id, P.prod_name FROM Products AS P;
SELECT C.cust_name AS KlantNaam, C.cust_contact AS KlantContact, P.prod_name AS ProductNaam, V.vend_name AS Leverancier FROM Customers AS C INNER JOIN Orders AS O ON C.cust_id = O.cust_id INNER JOIN OrderItems AS OI ON O.order_num = OI.order_num INNER JOIN Products AS P ON OI.prod_id = P.prod_id INNER JOIN Vendors AS V ON P.vend_id = V.vend_id WHERE OI.prod_id = 'RGAN01';
-- Oracle / MySQL / SQL Server SELECT C.cust_name KlantNaam, C.cust_contact KlantContact, P.prod_name ProductNaam, V.vend_name Leverancier FROM Customers C INNER JOIN Orders O ON C.cust_id = O.cust_id INNER JOIN OrderItems OI ON O.order_num = OI.order_num INNER JOIN Products P ON OI.prod_id = P.prod_id INNER JOIN Vendors V ON P.vend_id = V.vend_id WHERE OI.prod_id = 'RGAN01';
Experienced SQL users often omit AS for table aliases
| Different types of joins | |
|---|---|
| Type | Description |
| Self Join | A join of a table to itself; aliases let the same table be referenced more than once |
| Inner Join | A join to another table that returns rows where the keys match |
| Left outer Join | A join to another table that returns matching rows plus unmatched rows from the first table in FROM |
| Right outer Join | A join to another table that returns matching rows plus unmatched rows from the joined table |
| Full outer Join | A join to another table that returns matching rows plus unmatched rows from both tables |
Select all customer contacts who work for the same company as 'Jim Jones'
Using a subquery
SELECT cust_id, cust_name, cust_contact FROM Customers WHERE cust_name = ( SELECT cust_name FROM Customers WHERE cust_contact = 'Jim Jones' ) ;
SELECT c1.cust_id, c1.cust_name, c1.cust_contact FROM Customers c1, Customers c2 WHERE c1.cust_name = c2.cust_name AND c2.cust_contact = 'Jim Jones';
SELECT c1.cust_id, c1.cust_name, c1.cust_contact FROM Customers c1 INNER JOIN Customers c2 ON c1.cust_name = c2.cust_name WHERE c2.cust_contact = 'Jim Jones';
| cust_id | cust_name | cust_contact |
| 1000000003 | Fun4All | Jim Jones |
| 1000000004 | Fun4All | Denise L. Stephens |
Inner join
SELECT <column1>, <column2> FROM <table1> INNER JOIN <table2> ON <table1>.<key> = <table2>.<key>
Left Outer join
Right Outer join
SELECT <column1>, <column2> FROM <tableA> LEFT OUTER JOIN <tableB> ON <tableA>.<key> = <tableB>.<key>
SELECT <column1>, <column2> FROM <tableA> RIGHT OUTER JOIN <tableB> ON <tableA>.<key> = <tableB>.<key>
SELECT <column1>, <column2> FROM <tableA> LEFT OUTER JOIN <tableB> ON <tableA>.<key> = <tableB>.<key>
SELECT <column1>, <column2> FROM <tableB> RIGHT OUTER JOIN <tableA> ON <tableA>.<key> = <tableB>.<key>
Select the names of vendors supplying products priced below 6
-- Oracle / MySQL / SQL Server SELECT DISTINCT V.vend_name FROM Vendors V INNER JOIN Products P ON V.vend_id = P.vend_id WHERE P.prod_price < 6;
Using an INNER JOIN
-- Oracle / MySQL / SQL Server SELECT DISTINCT V.vend_name FROM Vendors V LEFT OUTER JOIN Products P ON V.vend_id = P.vend_id WHERE P.prod_price < 6;
Using a LEFT OUTER JOIN
| vend_name |
| Doll House Inc. |
| Bears R Us |
A LEFT OUTER JOIN can retrieve more rows than this question requires.
Retrieving unnecessary data adds cost and is best avoided.
Select the names of customers who have and have not placed an order, together with the corresponding order numbers.
-- Oracle / MySQL / SQL Server SELECT C.cust_name, O.order_num FROM Customers C LEFT OUTER JOIN Orders O ON C.cust_id = O.cust_id;
| cust_name | order_num |
| Village Toys | 20005 |
| Village Toys | 20009 |
| Kids Place | NULL |
| Fun4All | 20006 |
| Fun4All | 20007 |
| The Toy Store | 20008 |
Full Outer join
SELECT <column1>, <column2> FROM <tableA> FULL OUTER JOIN <tableB> ON <tableA>.<key> = <tableB>.<key>