Database Management/Advanced SQL

From Wikiversity
Jump to navigation Jump to search

This lesson introduces advanced SQL concepts, including grouping and sorting.

Objectives and Skills[edit | edit source]

Objectives and skills for this lesson include:

  • Understand advanced SQL concepts
  • Create SQL queries using GROUP BY and HAVING
  • Create SQL queries using ORDER BY
  • Create database manipulation language SQL queries using a database application

Readings[edit | edit source]

  1. Wikipedia: SQL
  2. Wikibooks: Structured Query Language/Data Query Language
  3. Wikibooks: Structured Query Language/Data Manipulation Language

Multimedia[edit | edit source]

  1. YouTube: Learn Basic SQL
  2. YouTube: The Structured Query Language (SQL)
  3. YouTube: Introduction to SQL with LibreOffice Base
  4. YouTube: Microsoft Access SQL Basics

Activities[edit | edit source]

  1. Select a database application to use for this lesson. LibreOffice Base and Microsoft Access are recommended.
  2. Review Database Applications and Database Software for background information and tutorials.
  3. Complete the activities below using your selected database application and SQL view.

LibreOffice Base[edit | edit source]

  1. To view SQL for a query, open the query and then select View and Switch Design View On/Off.
  2. To execute non-query SQL statements, use Tools / SQL. See LibreOffice: Executing SQL Commands for more information.

Microsoft Access[edit | edit source]

  1. To view SQL for a query, open the query and then select View and SQL View.

Northwind[edit | edit source]

  1. Download a copy of the Database Examples/Northwind database for your selected database application.
  2. Review the E-R diagram in your database application to verify that it matches the Database Examples/Northwind example. Note the primary keys, foreign keys, and relationships. Then write SQL queries to determine results for each of the following.
  3. Select the CustomerName and OrderID for all customers who ordered 'Aniseed Syrup', sorted in alphabetical order.
  4. Select the ProductName, Quantity, Price, and ExtendedPrice (Quantity * Price) for orders 10344 and 10345.
  5. Select the first and last names and current age of all employees having a birthday in September.
  6. Select the OrderID, count of products ordered, and total order cost for orders 10344 and 10345.
  7. Select the total number of orders and total cost of orders shipped by 'Speedy Express' in February 1997.
  8. Insert a new shipper with ID 4, name 'On Time Delivery', and phone '(503) 555 0123'.
  9. Update products to increase prices on all products by 1 ($1.00).
  10. Update products to reduce prices on all products by 1 (-$1.00).
  11. Update the new shipper's name from 'On Time Delivery' to 'On-Time Delivery'.
  12. Delete the new shipper.

Lesson Summary[edit | edit source]

  • The GROUP BY clause projects rows having common values into a smaller set of rows and is often used in conjunction with SQL aggregate functions or to eliminate duplicate rows from a result set.[1]
  • Aggregate functions include COUNT, SUM, MIN, MAX, and AVG.[2]
  • The HAVING clause includes a predicate used to filter rows resulting from the GROUP BY clause.[3]
  • The ORDER BY clause identifies which column[s] to use to sort the resulting data, and in which direction to sort them (ascending or descending).[4]
  • Without an ORDER BY clause, the order of rows returned by an SQL query is undefined.[5]
  • The DISTINCT keyword eliminates duplicate data.[6]
  • Data manipulation language (DML) includes the INSERT, UPDATE, and DELETE statements.[7]
  • The structure of the INSERT, UPDATE, and DELETE statements is:[8]
    • INSERT INTO ... VALUES ...
    • UPDATE ... SET ... WHERE ...
    • DELETE FROM ... WHERE ...

Key Terms[edit | edit source]

aggregate function
A function where the values of multiple rows are grouped together to form a single summary value.[9]

See Also[edit | edit source]

References[edit | edit source]