Arizona State University
W. P. Carey School of Business
CIS 360
The NorthWind Database Tutorial
PART 3 – Subqueries & Joins
In this lesson of the NorthWind SQL tutorial, you will learn...
1. To write queries with subqueries.
2. To select columns from multiple tables with joins.
3. To select records from multiple tables with unions.
Subqueries
Subqueries are queries embedded in queries. They are used to retrieve data from one table based on data in another table. They generally are used when tables have some kind of relationship. For example, in the NorthWind database, the Orders table has a
CustomerID field, which references a customer in the Customers table. Retrieving the
CustomerID for a specific order is pretty straightforward.
Code
…show more content…
Table Aliases
Using full table names as prefixes can make SQL queries unnecessarily wordy. Table aliases can make the code a little more concise. The example below, which is identical in functionality to the query above, illustrates the use of table aliases.
6
Code Sample 6
-- Create a report showing employee orders using Aliases.
SELECT e.EmployeeID, e.FirstName, e.LastName,
o.OrderID, o.OrderDate
FROM Employees e JOIN Orders o ON
(e.EmployeeID = o.EmployeeID)
ORDER BY o.OrderDate;
Multi-table Joins
Multi-table joins can get very complex and may also take a long time to process, but the syntax is relatively straightforward.
Syntax
SELECT table1.column, table2.column, table3.column
FROM table1
JOIN table2 ON (table1.column=table2.column)
JOIN table3 ON (table2.column=table3.column)
WHERE conditions
Note that, to join with a table, that table must be in the FROM clause or must already be joined with the table in the FROM clause. Consider the following.
SELECT table1.column, table2.column, table3.column
FROM table1
JOIN table3 ON (table2.column=table3.column)
JOIN table2 ON (table1.column=table2.column)
WHERE conditions
The above code would break because it attempts to join table3 with table2 before table2 has been joined with table1.
Code Sample 7
/*
Create a report showing the Order ID, the name of the company that
placed
The use of huge databases that combine all of a company 's data and allow users to access the data directly, create reports, and obtain responses to what-if questions is referred to as:
In order to complete this task, I will need the data that needs to be inserted into the table, and how the information will be used.
- Tables are useful in showing relationships and making comparisons. They should have title, row, column headings, population, source and explanations.
The lab begins with a simple example of query development using Access; then, evolves to more complex queries which the student should perform after completing the first exercise. The student can create a query with the wizard, with query design view, or with SQL statements. The Northwind database will be used again in this lab.
Unique Column and Table Names: A column name within the table is unique because it is referenced by its table name. RDBMS does not restrict other tables to use same column names because they are identified by their table. Similarly table names within a database needs to be
in the tables. I then came to a solution that worked for most tables but not all,
If we consider an example of a database for billing the claims to the provider, we have two tables in the database as ‘Claims’ and ‘Provider’. ‘Claims’ have the claim information such as (ClaimNumber, ClaimType, Type of service, Admission details) and ‘Provider’ have the provider information such as (ProviderID, FederalTaxID, NPI, PointOfService). The primary keys for ‘Claims’ is “ClaimNumber” and for ‘Provider’ is “ProviderID”.
2- Order Table - The Order Table is used to record information pertinent to each Order placed with a given store. Each order will have one or more entries in the Order Line Table associated with it.
The challenging part was actually trying to understand what will be the best way to break a relation down into multiple relations without jeopardizing the integrity of the data at the same time. As I mention before it is not hard to do it but you really have to know how to do it.
When it comes to the data model, there exists a relationship that has three different representations for the reason that database requires the relationship between the tables. It goes hand-in-hand with one another without the relationship the tables would have no purpose. The information cannot be repetitive in order for the each table to work and provide the specific database that is related to the information. In different ways the tables in the Huffman Trucking Fleet Truck database
The example discussed above on Customers table and Orders table is a one-to-many relationship. As each customer places multiple orders, each order only belongs to one customer. When creating a relationship between two tables, if only one of the related fields is unique, a one-to-many relationship is automatically set.
Verify that the field names shown are the common fields for the relationship. If a field name is incorrect, click on the field name and select the appropriate field from the list.
All of the information that I needed for the query was functionally routed through that table. It makes me wonder how convoluted query statements could become when trying to link together information from more complex relationships.