2Retrieving data from more than one table (joining tables) SELECT CUSTOMER_NUM, CUSTOMER_NAME, REP.REP_NUM, LAST_NAME, FIRST_NAME FROM CUSTOMER, REP WHERE CUSTOMER.REP_NUM = REP.REP_NUM;
3Setting restrictions on queries SELECT CUSTOMER_NUM, CUSTOMER_NAME, REP.REP_NUM, LAST_NAME, FIRST_NAME FROM CUSTOMER, REP WHERE CUSTOMER.REP_NUM = REP.REP_NUM AND CREDIT_LIMIT = 7500;
4There is more than one way to write a query and get the same result SELECT DESCRIPTION FROM ORDER_LINE, PART WHERE ORDER_LINE.PART_NUM = PART.PART_NUM AND ORDER_NUM = ‘21610’; Or if you prefer you may use a different format:
5Another example of the same query result SELECT DESCRIPTION FROM PART WHERE PART_NUM IN (SELECT PART_NUM FROM ORDER_LINE WHERE ORDER_NUM = ‘21610’); The subquery is considered first.
6A Shortcut to writing out long table names in queries SELECT R.REP_NUM, LAST_NAME, FIRST_NAME, C.CUSTOMER_NUM, CUSTOMER_NAME FROM REP R, CUSTOMER C WHERE R.REP_NUM = C.REP_NUM;
7Steps to writing a complex query List in the SELECT clause all of the columns/attributes you want to display. If the name of a column is in more than one table, qualify the name by stating which table it is fromList in the FROM class all the tables involved in the query. These are usually the tables that contain the data in the SELECT clause, but be aware if you need to restrict the data by using data in ANOTHER table in a WHERE clause, you will need to include it in the FROM clause.
8Steps to writing a complex query Take ONE pair of related tables at a time and indicate in the WHERE clause the condition that relates the tables. Join these conditions with the AND operator. If there are any conditions, include them in the WHERE clause and connect them to the other conditions with the AND operator.
9Example: For each part on order, list the part number, number ordered, order number, order date, customer number and name, and the last name of the sales rep.SELECT PART_NUM, NUM_ORDERED, ORDER_LINE.ORDER_NUM, ORDER_DATE, CUSTOMER.CUSTOMER_NUM, CUSTOMER, LAST_NAMEHERE WE STATE ORDER_LINE BEFORE ORDER_NUM BECAUSE ORDER_NUM IS IN BOTH THE ORDERS AND ORDER_LINE TABLE. WE WANT TO USE THE VALUE IN THE ORDER_LINE TABLE. THE SAME SITUATION APPLIES TO CUSTOMER_NUM. IT EXISTS IN THE CUSTOMER AND ORDERS TABLE.
10FROM ORDER_LINE, ORDERS, CUSTOMER, REP Example: For each part on order, list the part number, number ordered, order number, order data, customer number and name, and the last name of the sales rep.FROM ORDER_LINE, ORDERS, CUSTOMER, REPHERE ARE ALL THE TABLES NEEDED TO EXTRACT THE DATA I WANT.WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM AND CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM AND REP.REP_NUM = CUSTOMER.REP_NUMHERE YOU ARE CONNECTING THE DATA IN THE TABLES
12SET OPERATIONS -UNIONYou can combine two tables if they have the identical structure. This is usually done by creating selected data from a table based on a condition using two queries and then combining the results.
13First, design the query for the first restriction: List the number and name of each customer that either is represented by sales rep 65 or that currently has orders on file, or both.First, design the query for the first restriction:SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMERWHERE REP_NUM = ‘65’Secondly, design the query for the second restriction using the same structureSELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAMEFROM CUSTOMER, ORDERSWHERE CUSTOMER_NUM = ORDERS.CUSTOMER_NUM
14Now put them together with the UNION operator SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER WHERE REP_NUM = ‘65 UNION SELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER, ORDERS WHERE CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM;
15The UNION operatorMySQL will delete duplicates when you use the UNION operator.Note: I do not use INTERSECT, MINUS, or ALL and ANY. They tend to be “shortcuts” that cause more confusion than they are worth! : )
16SPECIAL OPERATIONSInner Join – These are the queries you are already familiar with. They retrieve only the data that you request that satisfies any WHERE clause restrictions.
17SPECIAL OPERATIONSOuter Join: These queries list all of the rows from one of the tables in the join regardless of whether they match the restriction criteria in the WHERE clause. A LEFT outer join includes all the rows from the table on the left (always the first table) of the tables listed.In a RIGHT outer join, all rows from the table on the right (the last table listed) are listed.In a FULL outer join, all rows from both tables are included regardless of whether they match rows from another table. I have NEVER used such a query!!!
18SPECIAL OPERATIONSProduct – A combination of all rows in the first table and second table. You only select TWO tables. List the attributes you wish to retrieve and the tables that contain them. Leave off the WHERE clause. BEWARE: You can mistakenly make a product by accidently omitting the WHERE clause!SELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME, ORDER_NUM, ORDER_DATE FROM CUSTOMER, ORDERS; See result on page 155