Sunteți pe pagina 1din 9

Queries examples with tables by Nasir Hussain

Basic:
SELECT [DISTINCT | ALL] [*] [Column expression] [AS newname]
FROM TableName [alias] [….] WHERE [condition]
GROUP BY [column list] [having condition]
ORDER BY [column list]

Case 1 (*)
To select all fields or columns of a table Branch
Branch
branchNo street city postcode
BOO4 32Manse Rd Bristol BS99 INZ
B002 56 Clover Dr London NW106EU
B003 163 Main St Glasgow G119QX
B007 16 Argyll St Aberdeen AB235U
BOO5 22 Deer Rd London SWI 4 EH

SQL Statement:
SELECT * FROM Branch;
Result:
Branch
branchNo street city postcode
BOO4 32Manse Rd Bristol BS99 INZ
B002 56 Clover Dr London NW106EU
B003 163 Main St Glasgow G119QX
B007 16 Argyll St Aberdeen AB235U
BOO5 22 Deer Rd London SWI 4 EH

Case 2 (DISTINCT)
i) To select different rows without repetition. Table PropertyForRent

PropertyForRent
City Postcode type rooms rent ownerNo
Aberdeen AB75SU House 6 650 CO46
Glasgow G129AX Flat 4 450 CO93
Glasgow G12 House 5 600 CO87
Glasgow G32 4QX Flat 3 375 CO93
Glasgow G119QX Flat 3 350 CO40
London NW2 Flat 4 400 CO87

SQL Statement:
SELECT DISTINCT ownerNo
FROM PropertyForRent;
Result:
ownerNo
CO40
CO46
CO87
CO93
So the row CO93 and CO87 are repeated so they will display only once without repeating
by default it display result in ascending order.
If we write it as
SQL Statement:
SELECT ownerNo
FROM PropertyForRent;
Result:
ownerNo
CO46
CO93
CO87
CO93
CO40
CO87
It also selects repeating rows.
ii) To select more than one attributes or columns. Table PropertyForRent
SQL Statement:
SELECT ownerNo,rent,rooms FROM PropertyForRent;

Query1
rent rooms ownerNo
650 6 CO46
450 4 CO93
600 5 CO87
375 3 CO93
350 3 CO40
400 4 CO87

Case 3 (ALL)
To select all fields or columns of a table showing also repetition table PropertyForRent
SQL Statement:
SELECT ALL ownerNo
FROM PropertyForRent;
Result:

ALL
ownerNo
CO46
CO93
CO87
CO93
ALL
ownerNo
CO46
CO40
CO87

Case 4 (WHERE)
i) To select any field having some condition table Client.

Client
clientNo fName lName telNo prefType maxRent
CR56 Aline Stewart 0141-848-1825 Flat 350
CR62 Mary Tregear 01224-196720 Flat 600
CR74 Mike Ritchie 01475-392178 Flat 750
CR76 john Kay 0207-774-5632 Flat 425

SQL Statement:
SELECT clientNo
FROM Client
WHERE maxrent >500;
Result:
Queryname
clientNo
CR74
CR62

ii) To select a particular record you would mention the particular key attribute name.
Table Guests
Guests
id name City country religion phone
1 Nasir Lahore Pakistan Islam 0321456789
2 Talal Mandi India Islam 0321456798
3 Ali London America christ 0421567894

4 Raza Mirpur Rusia hindu 3248705124


5 Shaiss Sialkot Italy islam 332451435
6 John Karachi Israel jews 234035735

7 Naveed Lahore Pakistan Islam 03436042573

SQL Statement:
SELECT Guests.id, Guests.name, Guests.city, Guests.country, Guests.religion,
Guests.phone
FROM Guests
WHERE Guests.id=2;
Result:
record
Id name city country religion phone
2 talal Mandi India Islam 0321456798
OR
SELECT *
FROM Guests
WHERE id =2;
The result will be same as above.
Case 5 (AS)
i) To select or display the result by changing the name of relation then you would
use the AS in your sql statement. We use the AS when the name of relation is so long then
we change the name and assign it alias.
Table PropertyForRent

PropertyForRent
City Postcode type rooms rent ownerNo
Aberdeen AB75SU House 6 650 CO46
Glasgow G129AX Flat 4 450 CO93
Glasgow G12 House 5 600 CO87
Glasgow G32 4QX Flat 3 375 CO93
Glasgow G119QX Flat 3 350 CO40
London NW2 Flat 4 400 CO87
SQL Statement:
SELECT * FROM PropertyForRent AS pfr;
Result:
Will be the above table.
ii) Table Guests

Guests
Id name city country religion phone
1 Nasir Lahore Pakistan Islam 0321456789
2 Talal Mandi India Islam 0321456798
3 Ali London America christ 0421567894
4 Raza Mirpur Rusia hindu 3248705124
5 Shaiss Sialkot Italy sekh 332451435
6 John Karachi Israel jews 234035735
7 Naveed Lahore Pakistan Islam 03436042573
SQL Statement:
SELECT *
FROM Guests AS g
WHERE city=Lahore;
Result:
AS city
id name city country religion phone
1 Nasir Lahore Pakistan Islam 0321456789
7 Naveed Lahore Pakistan Islam 03436042573
The above query also shows the working of parametric query.
Case 6 (ORDER BY)
i) This command is used to display result in an order either ascending or descending
order ASCENDING Table PropertyForRent

PropertyForRent
City Postcode type rooms rent ownerNo
Aberdeen AB75SU House 6 650 CO46
Glasgow G129AX Flat 4 450 CO93
Glasgow G12 House 5 600 CO87
Glasgow G32 4QX Flat 3 375 CO93
Glasgow G119QX Flat 3 350 CO40
London NW2 Flat 4 400 CO87

SQL Statement:
SELECT *
FROM PropertyForRent
ORDER BY rent ASC;
Result:
ascending
propertyNo street city postcode type rooms rent ownerNo
PG4 6 Lawrence St Glasgow G119QX Flat 3 350 CO40
PG36 2 Manor Rd Glasgow G32 4QX Flat 3 375 CO93
PL94 6 Argyll St London NW2 Flat 4 400 CO87
PG16 5 Novar Dr Glasgow G129AX Flat 4 450 CO93
PG21 18 Dale Rd Glasgow G12 House 5 600 CO87
PA14 16 Holhead Aberdeen AB75SU House 6 650 CO46

ii) DESCENDING ORDER


Table PrivateOwner
PrivateOwner
ownerNo fName lName address telNo
CO40 Tina Murphry 63 Well St, Glasgow,G42 0141-943-1728
CO46 Joe Keogh 2 Fergus Dr,Aberdeen AB2 7SX 01224-861212
CO87 Carol Farrel 6 Achray St, Glasgow G32 9DX 0141-357-7419
CO93 Tony Shaw 12 Park Pl, Glasgow G4 0QR 0141-225-7025

SQL Statement:
SELECT *
FROM PrivateOwner
ORDER BY ownerNo DESC;
Result:

descending
ownerNo fName lName address telNo
CO93 Tony Shaw 12 Park Pl, Glasgow G4 0QR 0141-225-7025
CO87 Carol Farrel 6 Achray St, Glasgow G32 9DX 0141-357-7419
descending
ownerNo fName lName address telNo
CO93 Tony Shaw 12 Park Pl, Glasgow G4 0QR 0141-225-7025
CO46 joe Keogh 2 Fergus Dr,Aberdeen AB2 7SX 01224-861212
CO40 Tina Murphry 63 Well St, Glasgow,G42 0141-943-1728

Case 7 (Column Functions)


1. COUNT ( )
2. MAX ( )
3. MIN ( )
4. SUM ( )
5. AVG ( )
1. COUNT (name of columns)
This function is used to count the number of rows or records of a table. Table Client
Client
clientNo fName lName telNo prefType maxrent
CR56 Aline Stewart 0141-848-1825 Flat 350
CR62 Mary Tregear 01224-196720 Flat 600
CR74 Mike Ritchie 01475-392178 Flat 750
CR76 john Kay 0207-774-5632 Flat 425

SQL Statement:
SELECT COUNT(*)
FROM Client;

Result:

count
Expr1000
4
The above relation (Client) has four (4) rows or records.
2. MAX (name of columns)
This function is used to find the maximum value or amount from the relation. Table
PropertyForRent

PropertyForRent
City Postcode type rooms rent ownerNo
Aberdeen AB75SU House 6 650 CO46
Glasgow G129AX Flat 4 450 CO93
Glasgow G12 House 5 600 CO87
Glasgow G32 4QX Flat 3 375 CO93
Glasgow G119QX Flat 3 350 CO40
London NW2 Flat 4 400 CO87

SQL Statement:
SELECT MAX(rent)
FROM PropertyForRent;
Result:
max
Expr1000
650

The attribute rent has maximum 650.


3. MIN (name of column)
This function is used to find the minimum value from the relation table
PropertyForRent
SQL Statement:
SELECT MIN(rooms)
FROM PropertyForRent;
Result:
Min
Expr1000
3
Another
SQL Statement:
SELECT MIN(rent)
FROM PropertyForRent;
Result:
Min
Expr1000
350

4. SUM (name of column or field)


This function is used to find out the sum of given column or field table
PropertyForRent

PropertyForRent
City Postcode type rooms rent ownerNo
Aberdeen AB75SU House 6 650 CO46
Glasgow G129AX Flat 4 450 CO93
Glasgow G12 House 5 600 CO87
Glasgow G32 4QX Flat 3 375 CO93
Glasgow G119QX Flat 3 350 CO40
London NW2 Flat 4 400 CO87

SQL Statement:
SELECT SUM(rooms)
FROM PropertyForRent;
Result:
sum
Expr1000
25
If
SQL Statement:
SELECT SUM(rent)
FROM PropertyForRent;
Result:
sum
Expr1000
2825

5. AVG (name of attribute)


This function is used to find the average of any column or field specified in the
parenthesis. Table name PropertyForRent

SQL Statement:
SELECT AVG(rent)
FROM PropertyForRent;

Result:
Avg
Expr1000
470.833333333333

If
SQL Statement:
SELECT AVG(rooms)
FROM PropertyForRent;
Result:

Avg
Expr1000
4.16666666666667

Case 8 (First and Last)


i) First (name of attribute)
This function is used to find out the First value of any given attribute name, Table
PropertyForRent. This function is also used for alpha numeric and strings.

PropertyForRent
City Postcode type rooms rent ownerNo
Aberdeen AB75SU House 6 650 CO46
Glasgow G129AX Flat 4 450 CO93
Glasgow G12 House 5 600 CO87
Glasgow G32 4QX Flat 3 375 CO93
Glasgow G119QX Flat 3 350 CO40
London NW2 Flat 4 400 CO87
SQL Statement:
SELECT First(rent)
FROM PropertyForRent;
Result:
Query1
Expr1000
650

If
SELECT First(rooms)
FROM PropertyForRent;
Result:
Query1
Expr1000
6

ii) Last (name of column)


This function is used to find out the 2nd value of any particular column or attribute table
PropertyForRent
SQL Statement:
SELECT Last (staffNo)
FROM PropertyForRent;
Result:
Query1
Expr1000
SG14

S-ar putea să vă placă și