queries examples with tables by nasir hussain

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:

Upload: nasir-hussain

Post on 10-Jun-2015

88 views

Category:

Documents


4 download

TRANSCRIPT

Queries examples with tables by Nasir HussainBasic: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

BranchbranchNo street city postcode

BOO4 32Manse Rd Bristol BS99 INZB002 56 Clover Dr London NW106EUB003 163 Main St Glasgow G119QXB007 16 Argyll St Aberdeen AB235UBOO5 22 Deer Rd London SWI 4 EH

SQL Statement:SELECT * FROM Branch;Result:

BranchbranchNo street city postcode

BOO4 32Manse Rd Bristol BS99 INZB002 56 Clover Dr London NW106EUB003 163 Main St Glasgow G119QXB007 16 Argyll St Aberdeen AB235UBOO5 22 Deer Rd London SWI 4 EH

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

PropertyForRentCity Postcode type rooms rent ownerNo

Aberdeen AB75SU House 6 650 CO46Glasgow G129AX Flat 4 450 CO93Glasgow G12 House 5 600 CO87Glasgow G32 4QX Flat 3 375 CO93Glasgow G119QX Flat 3 350 CO40London NW2 Flat 4 400 CO87

SQL Statement:SELECT DISTINCT ownerNoFROM PropertyForRent;Result:

ownerNoCO40CO46CO87CO93

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 asSQL Statement:SELECT ownerNoFROM PropertyForRent;Result:

ownerNoCO46CO93CO87CO93CO40CO87

It also selects repeating rows.ii) To select more than one attributes or columns. Table PropertyForRentSQL Statement:SELECT ownerNo,rent,rooms FROM PropertyForRent;

Query1rent rooms ownerNo650 6 CO46450 4 CO93600 5 CO87375 3 CO93350 3 CO40400 4 CO87

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

ALLownerNoCO46CO93CO87CO93

ALLownerNoCO46CO40CO87

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

ClientclientNo fName lName telNo prefType maxRent

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

SQL Statement:SELECT clientNoFROM ClientWHERE maxrent >500;Result:

QuerynameclientNo

CR74CR62

ii) To select a particular record you would mention the particular key attribute name. Table Guests

Guestsid name City country religion phone1 Nasir Lahore Pakistan Islam 03214567892 Talal Mandi India Islam 03214567983 Ali London America christ 0421567894

4 Raza Mirpur Rusia hindu 32487051245 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.phoneFROM GuestsWHERE Guests.id=2;Result:

recordId name city country religion phone

2 talal Mandi India Islam 0321456798

ORSELECT *FROM GuestsWHERE 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

PropertyForRentCity Postcode type rooms rent ownerNo

Aberdeen AB75SU House 6 650 CO46Glasgow G129AX Flat 4 450 CO93Glasgow G12 House 5 600 CO87Glasgow G32 4QX Flat 3 375 CO93Glasgow G119QX Flat 3 350 CO40London NW2 Flat 4 400 CO87

SQL Statement:SELECT * FROM PropertyForRent AS pfr;Result:

Will be the above table.ii) Table Guests

GuestsId name city country religion phone1 Nasir Lahore Pakistan Islam 03214567892 Talal Mandi India Islam 03214567983 Ali London America christ 04215678944 Raza Mirpur Rusia hindu 32487051245 Shaiss Sialkot Italy sekh 3324514356 John Karachi Israel jews 2340357357 Naveed Lahore Pakistan Islam 03436042573

SQL Statement:SELECT *FROM Guests AS gWHERE city=Lahore;Result:

AS cityid name city country religion phone

1 Nasir Lahore Pakistan Islam 03214567897 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

PropertyForRentCity Postcode type rooms rent ownerNo

Aberdeen AB75SU House 6 650 CO46Glasgow G129AX Flat 4 450 CO93Glasgow G12 House 5 600 CO87Glasgow G32 4QX Flat 3 375 CO93Glasgow G119QX Flat 3 350 CO40London NW2 Flat 4 400 CO87

SQL Statement:SELECT *FROM PropertyForRentORDER BY rent ASC;Result:

ascendingpropertyNo street city postcode type rooms rent ownerNo

PG4 6 Lawrence St Glasgow G119QX Flat 3 350 CO40PG36 2 Manor Rd Glasgow G32 4QX Flat 3 375 CO93PL94 6 Argyll St London NW2 Flat 4 400 CO87PG16 5 Novar Dr Glasgow G129AX Flat 4 450 CO93PG21 18 Dale Rd Glasgow G12 House 5 600 CO87PA14 16 Holhead Aberdeen AB75SU House 6 650 CO46

ii) DESCENDING ORDERTable PrivateOwner

PrivateOwnerownerNo fName lName address telNo

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

SQL Statement:SELECT *FROM PrivateOwnerORDER BY ownerNo DESC;Result:

descendingownerNo fName lName address telNo

CO93 Tony Shaw 12 Park Pl, Glasgow G4 0QR 0141-225-7025CO87 Carol Farrel 6 Achray St, Glasgow G32 9DX 0141-357-7419

descendingownerNo fName lName address telNo

CO93 Tony Shaw 12 Park Pl, Glasgow G4 0QR 0141-225-7025CO46 joe Keogh 2 Fergus Dr,Aberdeen AB2 7SX 01224-861212CO40 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

ClientclientNo fName lName telNo prefType maxrent

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

SQL Statement:SELECT COUNT(*)FROM Client;

Result:

countExpr1000

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

PropertyForRentCity Postcode type rooms rent ownerNo

Aberdeen AB75SU House 6 650 CO46Glasgow G129AX Flat 4 450 CO93Glasgow G12 House 5 600 CO87Glasgow G32 4QX Flat 3 375 CO93Glasgow G119QX Flat 3 350 CO40London NW2 Flat 4 400 CO87

SQL Statement:SELECT MAX(rent)

FROM PropertyForRent;Result:

maxExpr1000650

The attribute rent has maximum 650.3. MIN (name of column)This function is used to find the minimum value from the relation table PropertyForRentSQL Statement:SELECT MIN(rooms)FROM PropertyForRent; Result:

MinExpr10003

Another SQL Statement:SELECT MIN(rent)FROM PropertyForRent;Result:

MinExpr1000350

4. SUM (name of column or field)This function is used to find out the sum of given column or field table PropertyForRent

PropertyForRentCity Postcode type rooms rent ownerNo

Aberdeen AB75SU House 6 650 CO46Glasgow G129AX Flat 4 450 CO93Glasgow G12 House 5 600 CO87Glasgow G32 4QX Flat 3 375 CO93Glasgow G119QX Flat 3 350 CO40London NW2 Flat 4 400 CO87

SQL Statement:SELECT SUM(rooms)FROM PropertyForRent;Result:

sumExpr1000

25

If

SQL Statement:SELECT SUM(rent)FROM PropertyForRent;Result:

sumExpr1000

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

Expr1000470.833333333333

IfSQL Statement:SELECT AVG(rooms)FROM PropertyForRent;Result:

AvgExpr1000

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.

PropertyForRentCity Postcode type rooms rent ownerNo

Aberdeen AB75SU House 6 650 CO46Glasgow G129AX Flat 4 450 CO93Glasgow G12 House 5 600 CO87Glasgow G32 4QX Flat 3 375 CO93Glasgow G119QX Flat 3 350 CO40London NW2 Flat 4 400 CO87

SQL Statement:SELECT First(rent)FROM PropertyForRent;Result:Query1Expr1000650

IfSELECT First(rooms)FROM PropertyForRent;Result:Query1Expr10006

ii) Last (name of column)This function is used to find out the 2nd value of any particular column or attribute table PropertyForRentSQL Statement:SELECT Last (staffNo)FROM PropertyForRent;Result:Query1Expr1000SG14