# Database Management/Query by Example

This lesson introduces Query by Example (QBE). QBE is a database query language for relational databases, using visual tables where the user enters commands, example elements and conditions.

## Objectives and Skills

Objectives and skills for this lesson include:
* Understand QBE concepts
* Create single-table QBE queries using a database application
* Create multi-table QBE queries using a database application

## Readings

* Wikipedia: Query by Example
* Wikipedia: LibreOffice Base
* Wikipedia: Microsoft Access
* Wikibooks: Microsoft Office/Create queries for a database

## Multimedia
### QBE

* YouTube: Running Queries: Query by Example (QBE)
* YouTube: Query By Example

### LibreOffice Base

* YouTube: Getting Started with LibreOffice Base
* YouTube: LibreOffice Base Introduction to Queries

### Microsoft Access

* YouTube: Access - Getting Started
* YouTubeː Microsoft Access - Tutorial for Beginners
* YouTube: How to Create a Calculation Query in Microsoft Access
* Microsoft: Using date criteria in queries

## Activities

* 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.

### LibreOffice Base

* For Linux, MacOS, and Windows. Download and install the free and open LibreOffice suite.
* Complete one or more of the following tutorials using LibreOffice Base:
** ELearn: LibreOffice Queries
** Tech Republic: How to Query a LibreOffice Database Using the Query Design View
** LibreOffice.org: Getting Started with Base - Creating Queries
* To view the database E-R diagram, select Tools and Relationships.
* To view SQL for a query, open the query and then select View and Switch Design View On/Off.

### Microsoft Access

* For Windows only. If you don't already have Microsoft Access installed, you can sign up for the free Microsoft: Azure for Students. Within the Microsoft: Azure Education Software portal, there is an option to download Access 2016.
* Complete the following tutorials using Microsoft Access:
** GCF Global: Access - Introduction to Databases
** GCF Global: Access - Introduction to Objects
** GCF Global: Access - Designing a Simple Query
** GCF Global: Access - Designing a Multi-Table Query
** GCF Global: Access - More Query Design Options
* To view the database E-R diagram, select Database Tools and Relationships.
* To view SQL for a query, open the query and then select View and SQL View.

### Northwind

* 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 create QBE queries to determine results for each of the following. View the SQL generated for each query.
* Select all fields and all records in the Categories table.
* Select the CustomerName for all customers.
* Select the CustomerName for all customers in the country 'Italy'.
* Select the ProductName for all products in Category 1 with a price less than $5.00.
* Select the SupplierName for all suppliers from English-speaking countries (Australia, Canada, UK, USA)
* Select the first and last names of all employees having a birthday in September.
* Select the CustomerName and OrderID for all orders placed on 9 September 1996.
* Select the ProductName for all seafood products with a supplier from 'Boston'.
* 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.

### Pubs

* Download a copy of the Database Examples/Pubs database for your selected database application.
* Review the E-R diagram in your database application to verify that it matches the Database Examples/Pubs example. Note the primary keys, foreign keys, and relationships. Then create QBE queries to determine results for each of the following. View the SQL generated for each query.
* Select all fields and all records in the Authors table.
* Select the emp_id and lname for all employees.
* Select the au_firstname and au_lastname for all authors in the state 'UT'.
* Select the orderid for all orders in storeid 7131 with a quantity greater than 20.
* Select the publishername for all publishers in the European-speaking countries (Germany, France).
* Select the first and last names of all employees having a hire date in June.
* Select the orderid, ord_detail, and title for all orders placed on September 13, 2024.
* Select the first and last employee name for all job descriptions with a publisher name from 'NY'.
* Select the storename and orderid for all stores that ordered 'The Busy Executive's Database Guide', sorted in alphabetical order.
* Select the title, quantity, price, and extendedprice (quantity * price) for sales orders P2121 and P723
* Select the first and last names and current number of years for all employees having a hire date in November.
* Select the order number, sum of quantity sales ordered, and total extended cost for orders P2121 and P723.
* Select the total number of orders and total cost of orders shipped by 'Bookbeat' between January - May 2023.

## Lesson Summary

* Query by Example (QBE) is a database query language for relational databases.
* QBE is a graphical query language, using visual tables where the user enters commands, example elements and conditions.
* QBE supports retrieving data, inserts, deletes, and updates, as well as creation of temporary tables.
* Behind the scenes, QBE converts the user's actions into statements expressed in a database manipulation language, such as SQL. It is this statement that is actually executed.
* It is easier and more productive for end-users (and even programmers) to select tables and columns by selecting them rather than typing in their names.
* LibreOffice Base is a free and open-source relational database management system that is part of the LibreOffice office suite.
* LibreOffice Base is designed to allow users to easily create, access, modify, and view databases and their data.
* LibreOffice Base provides users with a graphical user interface that allows users to work with four main tools: tables, queries, forms, and reports.
* Microsoft Access is a database management system (DBMS) from Microsoft that combines the relational Microsoft Jet Database Engine with a graphical user interface and software-development tools.
* Microsoft Access users can create tables, queries, forms and reports, and connect them together with macros. Advanced users can use Visual Basic for Applications (VBA) to write rich solutions with advanced data manipulation and user control.

## Key Terms
### form

    Provides an attractive layout used for entering or looking up data on screen.

### join

    Combines columns from one or more tables into a new logical table or view.

### query

    Provides the ability to access or modify data by asking questions.

### report

    Provides an attractive layout used for printing out data with sophisticated presentation.

### table

    Stores information about one specific kind of item.

## See Also

* Database Applications
* Database Software
* Harper College: Relational Databases and Microsoft Access
* GCFGlobal: Access Basics
* Geeks For Geeks: Query By Example
* Microsoft: Examples of query criteria
* Microsoft: Access Functions (by Category)
* Microsoft: Sum data using a query