Preview

The NorthWind Database Tutorial

Good Essays
Open Document
Open Document
1885 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
The NorthWind Database Tutorial
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 Sample 1
/*
Find the CustomerID of the company that placed order 10290.
*/
SELECT CustomerID
FROM Orders
WHERE OrderID = 10290;

Code Explanation
This will return COMMI, which is very likely meaningless to the people reading the report. The next query uses a subquery to return a meaningful result.

1

Code Sample 2
-- Find the name of the company that placed order 10290.
SELECT CompanyName
FROM Customers
WHERE CustomerID = (SELECT CustomerID
FROM Orders
WHERE OrderID = 10290);

Code Explanation
The above code returns Comercio Mineiro, which is a lot more useful than COMMI.
The subquery can contain any valid SELECT statement, but it must return a single column with the expected number of results. For example, if the subquery returns only one result, then the main query can check for equality, inequality, greater than, less than, etc. On the other hand, if the subquery returns more than one record, the main query must check to see if a field value is (or is NOT) IN the set of values returned.

Code Sample 3
-- Find the Companies that placed orders in 1997
/******************************
The query below will work in SQL Server
******************************/
SELECT CompanyName
FROM Customers
WHERE CustomerID IN (SELECT CustomerID

You May Also Find These Documents Helpful

  • Good Essays

    The SELECT statement is the primary means of extracting data from database tables, and allows you to determine exactly which data you want to extract by means of different comparison operators used in the WHERE clause. This includes the use of specific "wild card" characters which allow you to search for character or number patterns within the data. You can also perform mathematical expressions within the SELECT statement to create derived output. The ORDER BY clause allows you to sort the output data in either ascending (the default) or descending order. Lab #5 will explore all of these applications of the SELECT statement.…

    • 1559 Words
    • 7 Pages
    Good Essays
  • Satisfactory Essays

    3. Now, using the completed contingency table, select the statements from the following list that are true. Note: a statement is true only if the value you calculated from the completed contingency table, when rounded to the same number of decimal places as in the statement, is the same as the value in the statement.…

    • 867 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Homework Unit 3

    • 354 Words
    • 2 Pages

    6. And operator - If the number is not within the range then it comes back false.…

    • 354 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    unit 5 assignment 1

    • 423 Words
    • 3 Pages

    6. When determining if a number is within a range, the OR operator would be best to use.…

    • 423 Words
    • 3 Pages
    Satisfactory Essays
  • Satisfactory Essays

    | Select and apply criteria in a reasonable manner.Select and apply criteria in a reasonable manner.…

    • 734 Words
    • 3 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Unit 6 True

    • 287 Words
    • 1 Page

    The WHERE clause is used to set criteria by which to filter which rows are returned or affected. TRUE…

    • 287 Words
    • 1 Page
    Satisfactory Essays
  • Satisfactory Essays

    When you include multiple data sources in a query, you use joins to limit the records that you want to see, based on how the data sources are related to each other. You also use joins to combine records from both data sources, so that each pair of records from the sources becomes one record in the query results. I think that a cross-join would be optimized in the library, so that less data was sent over the network and then merged, so that data didn 't need to be combined.…

    • 337 Words
    • 1 Page
    Satisfactory Essays
  • Satisfactory Essays

    unit 6

    • 360 Words
    • 2 Pages

    13) The WHERE clause is used to set criteria by which to filter which rows are returned or affected.…

    • 360 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Unit 18 - Database P1, M1

    • 995 Words
    • 4 Pages

    A query is use for searching some specific record. It allow user to set conditions and then search the record which match the conditions. User also is able to make the search mix up with other table and sort the order of the result.…

    • 995 Words
    • 4 Pages
    Powerful Essays
  • Good Essays

    Unit 5 PT1420

    • 959 Words
    • 7 Pages

    The AND operator would be the best to use to determine whether or not a number is within a given range.…

    • 959 Words
    • 7 Pages
    Good Essays
  • Satisfactory Essays

    Hrm Labor

    • 363 Words
    • 2 Pages

    | (TCO 2) Which of the following functions would you use to extract the from your records in the database?…

    • 363 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    07

    • 4772 Words
    • 25 Pages

    1) The SQL CREATE TABLE statement is used to name a new table and describe the table's columns.…

    • 4772 Words
    • 25 Pages
    Good Essays
  • Good Essays

    * Each row of the table holds data that pertain to some entity or a portion of some entity…

    • 805 Words
    • 4 Pages
    Good Essays
  • Better Essays

    | |test, one needs to use an evaluation that measures only one |the same items for comparison so the responses should all |tests, such as a math test, but much more difficult for more |…

    • 1613 Words
    • 7 Pages
    Better Essays
  • Satisfactory Essays

    Relational Database Paper

    • 547 Words
    • 3 Pages

    A relationship is an important component of a relational database. A relationship exists between two tables when one or more key fields from one table are matched to one or more key fields in another table. The fields in both tables usually have the same name, data type, and size. It also helps to further refine table structures and minimize redundant data.…

    • 547 Words
    • 3 Pages
    Satisfactory Essays

Related Topics