Creating Queries: Comsats Institute of Information Technology Wah Campus Department of Computer Science

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH

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.

Instructor Manual/Database Management System

BS(CS)

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

After clicking on the Run button, the result of the query will be displayed in datasheet
view.

Change from Datasheet View to Query Design View


After you run a query, you can easily change back to Query Design view and make modifications
to your query or create a new query.
Instructor Manual/Database Management System

BS(CS)

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

Activate the Home tab.


Click the down-arrow below View in the Views group. A menu appears.
Click Design View. Access changes to Query Design view. You can modify your query.

Retrieve a Single Column


You can use an Access query to retrieve a single column of data. Instead of choosing the
tablename.* option on the Field line in Query Design view, choose the name of the field you want
to retrieve.

Instructor Manual/Database Management System

BS(CS)

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

Open a table or query in Query Design view.


Choose the field name you want to display in the field line.
Click the Run button. Access retrieves the column you chose.

Retrieve Multiple Columns


You can use an Access query to retrieve multiple columns of data. On the Field line in Query
Design view, choose the field name of each field you want to retrieve in the order you want to
Instructor Manual/Database Management System

BS(CS)

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

retrieve them.

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.
Click the Run button. Access retrieves the columns you chose.

Sort a Query
Instructor Manual/Database Management System

BS(CS)

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

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.

Instructor Manual/Database Management System

BS(CS)

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

Sort Multiple Columns in a Query


You can sort the rows your query returns. You can also create sorts within a sort. For
example, you can sort by first name and then within a first name, you can sort by last
name. You specify the sort in the order you want the sort to occur. Your sort order may not
agree with the order in
which you want to display fields. In such a case, you can use fields that do not display to
enter your sort order. To prevent a field from displaying, deselect the Show box on the
Show row.
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.
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.
Deselect the Show button for the 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.

Instructor Manual/Database Management System

BS(CS)

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

Create a Query That Uses Two or More Tables


If you want to view data from two or more tables or queries, you can create a query that pulls the
data from multiple tables or queries. The tables and queries from which you pull your data should
Instructor Manual/Database Management System

BS(CS)

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

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.

Instructor Manual/Database Management System

BS(CS)

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

Save the query for later use.

Instructor Manual/Database Management System

BS(CS)

10

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

Create a Parameter Query


If instead of entering predetermined criteria, you want to prompt users when a query runs, you
can create a parameter query. You create a parameter query by enclosing a question in square
brackets ([]). For example, if you want to create a parameter query that asks users which
Department they want to use from the Students table, you would type [Which Department?] on
the Criteria line under the Department column. When the query runs, Access will prompt the user
for the answer to your question.

Open a table or query in Query Design view.


Create your query.
On the Criteria line, type the prompt within square brackets.
Click the Run button. Access prompts to enter the value.

Instructor Manual/Database Management System

BS(CS)

11

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

Instructor Manual/Database Management System

BS(CS)

12

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

QUERY WRITING USING SQL:


Click on create optionquery design
Dont add tables

In sql view write querys.


Instructor Manual/Database Management System

BS(CS)

13

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

1) The SQL SELECT Statement


The SELECT statement is used to select data from a database.The result is stored in a
result table, called the result-set.

SQL SELECT Syntax


SELECT column_name,column_name
FROM table_name;
It will return specific columns .
And
SELECT * FROM table_name;
It will return whole table data.

SELECT Column Example:


SELECT CustomerName,City FROM Customers;
SELECT * Example:
SELECT * FROM Customers;

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.

SQL SELECT DISTINCT Syntax


SELECT DISTINCT column_name,column_name
FROM table_name;

SELECT DISTINCT Example


SELECT DISTINCT City FROM Customers;
Instructor Manual/Database Management System

BS(CS)

14

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

3)SQL WHERE Clause


The WHERE clause is used to filter records. The WHERE clause is used to extract only those
records that fulfill a specified criterion.

SQL WHERE Syntax


SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;

WHERE Clause Example


SELECT * FROM Customers
WHERE Country='Mexico';
Text Fields vs. Numeric Fields:
SQL requires single quotes around text values (most database systems will also allow
double quotes).
However, numeric fields should not be enclosed in quotes:
SELECT * FROM Customers
WHERE CustomerID=1;

4)SQL AND & OR Operators


The AND & OR operators are used to filter records based on more than one condition.
The AND operator displays a record if both the first condition AND the second condition are true.
The OR operator displays a record if either the first condition OR the second condition is true.

AND Operator Example


SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';

OR Operator Example
Instructor Manual/Database Management System

BS(CS)

15

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

SELECT * FROM Customers


WHERE City='Berlin'
OR City='Mnchen';

Combining AND & OR


SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='Mnchen');

5)SQL LIKE Operator


The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

SQL LIKE Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

SQL LIKE Operator Examples


The following SQL statement selects all customers with a City starting with the letter "s":
SELECT * FROM Customers
WHERE City LIKE 's%';
The following SQL statement selects all customers with a City ending with the letter "s":
SELECT * FROM Customers
WHERE City LIKE '%s';
The following SQL statement selects all customers with a Country containing the pattern
"land":
SELECT * FROM Customers
WHERE Country LIKE '%land%';
Using the NOT keyword allows you to select records that do NOT match the pattern.
Instructor Manual/Database Management System

BS(CS)

16

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

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');

7)SQL BETWEEN Operator


The BETWEEN operator is used to select values within a range. The values can be
numbers, text, or dates.

SQL BETWEEN Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

BETWEEN Operator Example


The following SQL statement selects all products with a price BETWEEN 10 and 20:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
Instructor Manual/Database Management System

BS(CS)

17

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

NOT BETWEEN Operator Example


SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
BETWEEN Operator with Text Value Example
SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M';

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

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

6. Identify the Primary Key(s) of this table (if there is any).


7. Create a form to enter details about books and save it as Book Copies Form.
8. Insert following records to the Book Copies table using Book Copies Form.

9. Establish the Relationship between Books and Book Copies tables.


10. Write a query called Query_Book category to display all books which belongs to
given category; E.g. Category 02.
11. Write a query called Query_Book ISBN to display book details which belongs to given
ISBN. Book Detail include ISBN, Version, Title, Description, Number of Copies
12. Create a report called Book Details using the following format and save it.

13. Create a report called Book Details ISBN of query Query_Book ISBN using report
wizard.
Instructor Manual/Database Management System

BS(CS)

19

COMSATS INSTITUTE OF INFORMATION TECHNOLOGY WAH


CAMPUS
Department Of Computer Science

14. Save your database again

Instructor Manual/Database Management System

BS(CS)

20

You might also like