Database Management/Advanced SQL
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]
- Wikipedia: SQL
- Wikibooks: Structured Query Language/Data Query Language
- Wikibooks: Structured Query Language/Data Manipulation Language
Multimedia[edit | edit source]
- YouTube: Learn Basic SQL
- YouTube: The Structured Query Language (SQL)
- YouTube: Introduction to SQL with LibreOffice Base
- YouTube: Microsoft Access SQL Basics
Activities[edit | edit source]
- Select a database application to use for this lesson. LibreOffice Base and Microsoft Access are recommended.
- Review Database Applications and Database Software for background information and tutorials.
- Complete the activities below using your selected database application and SQL view.
LibreOffice Base[edit | edit source]
- To view SQL for a query, open the query and then select
View
andSwitch Design View On/Off
. - To execute non-query SQL statements, use
Tools
/SQL
. See LibreOffice: Executing SQL Commands for more information.
Microsoft Access[edit | edit source]
- To view SQL for a query, open the query and then select
View
andSQL View
.
Northwind[edit | edit source]
- Download a copy of the Database Examples/Northwind database for your selected database application.
- 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.
- Select the CustomerName and OrderID for all customers who ordered 'Aniseed Syrup', sorted in alphabetical order.
- Select the ProductName, Quantity, Price, and ExtendedPrice (Quantity * Price) for orders 10344 and 10345.
- Select the first and last names and current age of all employees having a birthday in September.
- Select the OrderID, count of products ordered, and total order cost for orders 10344 and 10345.
- Select the total number of orders and total cost of orders shipped by 'Speedy Express' in February 1997.
- Insert a new shipper with ID 4, name 'On Time Delivery', and phone '(503) 555 0123'.
- Update products to increase prices on all products by 1 ($1.00).
- Update products to reduce prices on all products by 1 (-$1.00).
- Update the new shipper's name from 'On Time Delivery' to 'On-Time Delivery'.
- 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
, andAVG
.[2] - The
HAVING
clause includes a predicate used to filter rows resulting from theGROUP 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]
- SQL
- Wikibooks: Database Design/Fundamental Concepts
- W3Schools: SQL Tutorial
- SQL Course: Introduction
- Codecademy: SQL Commands
- McFadyen: Relational Databases and Microsoft Access - Chapter 9 (Data Definitional Language (DDL)
- Watt: Database Design - Chapter 15 (SQL)