Creating Queries: Comsats Institute of Information Technology Wah Campus Department of Computer Science
Creating Queries: Comsats Institute of Information Technology Wah Campus Department of Computer Science
Creating Queries: Comsats Institute of Information Technology Wah Campus Department of Computer Science
CAMPUS
Department Of Computer Science
LAB 10
Summary
Items
Course Title
Lab Title
Duration
Operating
System
/Tool/Language
Objective
Description
Database Management System
Exploring Microsoft Access
3 Hours
Windows Operating System/MS Access 2007
To get familiar with creating query in MS Access
Creating Queries
SQL is a standard language for accessing databases.
A query can be based on tables or on other queries. To create a query, you open the tables or
queries on which you are going to base your query in Query Design view, and then use the
options in Design view to create your query. Then click the Run button to display the results.
Save queries for later use.
Activate the Create tab.
Click the Query Design button in the other group. The Show Table dialog box appears.
Activate the Tables tab if you want to base your query on tables, activate the Queries tab if
you want base your query on queries or activate the Both tab if you want to base your
query on both tables and queries.
Click to choose the table or query on which you want to base your query.
Click Add. The table appears in the window.
Click to choose the next table or query on which you want to base your query.
Continue clicking tables or queries until you have all the tables and queries you plan to
use.
Click Close. Access changes to Query Design view.
BS(CS)
After clicking on the Run button, the result of the query will be displayed in datasheet
view.
BS(CS)
BS(CS)
BS(CS)
retrieve them.
Sort a Query
Instructor Manual/Database Management System
BS(CS)
When creating a query, you can sort the rows you retrieve in ascending or descending order
by choosing the option you want on the Sort row in Query Design view.
Open a table or query in Query Design view.
Choose the field names you want to retrieve in the order you want to retrieve them.
Under the field you want to sort, click the down-arrow and then choose Ascending or
Descending.
Click the Run button. Access retrieves the columns you chose and displays the rows in
the order you specified.
BS(CS)
BS(CS)
BS(CS)
have a relationship.
Open the tables and/or queries you want to use in Query Design view.
Choose the field names you want to retrieve in the order you want to retrieve them.
Choose the field names you want to sort by in the order you want to sort. Under the fields
you want to sort by, choose Ascending or Descending.
Enter your selection criteria, if necessary.
Deselect the Show button for columns you do not want to display
Click the Run button. Access retrieves the columns you chose and displays the rows in the
order you specified.
BS(CS)
BS(CS)
10
BS(CS)
11
BS(CS)
12
BS(CS)
13
2
2)SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
In a table, a column may contain many duplicate values; and sometimes you only want to list the
different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different) values.
BS(CS)
14
OR Operator Example
Instructor Manual/Database Management System
BS(CS)
15
BS(CS)
16
The following SQL statement selects all customers with Country NOT containing the pattern
"land":
Example
SELECT * FROM Customers
WHERE Country NOT LIKE '%land%';
6)SQL IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
IN Operator Example
The following SQL statement selects all customers with a City of "Paris" or "London":
SELECT * FROM Customers
WHERE City IN ('Paris','London');
BS(CS)
17
LAB TASK
1. You need to create a database for a public library to store book details. For this, create a
blank database and save it.
2. Create a table called Books with the following fields.
3. Create a form to enter details about books and save it as Books Form.
4. Add following records to the Books table using the Books Form.
5. Create another table called Book Copies with the following fields.
Instructor Manual/Database Management System
BS(CS)
18
13. Create a report called Book Details ISBN of query Query_Book ISBN using report
wizard.
Instructor Manual/Database Management System
BS(CS)
19
BS(CS)
20