39
Adavnced Database Programming 1 MS SQL Server 2005 MS SQL Server 2005 Lab # 3 : Practicing Queries

Sql server lab_3

Embed Size (px)

Citation preview

Page 1: Sql server lab_3

Adavnced Database Programming 1

MS SQL Server 2005MS SQL Server 2005

Lab # 3 : Practicing Queries

Page 2: Sql server lab_3

Adavnced Database Programming 2

1. Practicing Company Database Example

Create EmployeeEmployee Table: FNAME:FNAME: First Name LNAME:LNAME: Last Name SSN:SSN: Social Security Number (Primary Key) BDATE:BDATE: Birth Date ADDRESS:ADDRESS: Employee’s Address Gender:Gender: F (Female) OR M (Male) SALARYSALARY SUPERSSN:SUPERSSN: Employee’s Supervisor SSN DNO:DNO: No. of the Department the Emp. Works in (Foreign (Foreign

Key)Key)

Page 3: Sql server lab_3

Adavnced Database Programming 3

1. Practicing Company Database Example

Create DepartmentDepartment Table: DNAME:DNAME: Department Name DNUMBER:DNUMBER: Department Number (Primary Key) DLOCATION:DLOCATION: Department Location MGRSSN:MGRSSN: Manager Social Security Number

(Foreign Key)(Foreign Key) MGRSTARTDATE:MGRSTARTDATE: Manager Work Start Date

Page 4: Sql server lab_3

Adavnced Database Programming 4

1. Practicing Company Database Example

Create ProjectProject Table: PNAME:PNAME: Project Name PNUMBER:PNUMBER: Project Number (Primary Key) PLOCATION:PLOCATION: Project Location DNUM:DNUM: Department Number which Project

belongs to (Foreign Key)(Foreign Key)

Page 5: Sql server lab_3

Adavnced Database Programming 5

1. Practicing Company Database Example

Create Works_ONWorks_ON Table: ESSN:ESSN: Employee Social Security Number

(Primary Key) + (Foreign Key)(Foreign Key) PNO:PNO: Project Number (Primary Key) + (Foreign (Foreign

Key)Key) HOURS:HOURS: Project Working Hours

Page 6: Sql server lab_3

Adavnced Database Programming 6

2. Simple Queries

Basic SELECT Form:

USE database_name

SELECT [ ALL | DISTINCT ] Column_List

From table1[table2,….]

WHERE conditions

Page 7: Sql server lab_3

Adavnced Database Programming 7

2. Simple Queries

Basic SELECT Form:

Column_List Column_List Specifications:: * denotes for all columns of all tables

specified in the FROM Clause Specified Column name Column_Name as Column_Heading To

Replace OR to assign a new column name to an expression

An Expression, System or Aggregate Function

Page 8: Sql server lab_3

Adavnced Database Programming 8

2. Simple Queries

Example :Q:Q: View all Departments info detailsA:A:

Use CompanySelect *From department

ORUse CompanySelect DNAME,DNUMBER,DLOCATION,

MGRSSN,MGRSTARTDATEFrom department

Page 9: Sql server lab_3

Adavnced Database Programming 9

2. Simple Queries

Example :Q:Q: View all Employees Salary values

A:A:Use CompanySelect ALL SalaryFrom employee

Q:Q: View Distinct Employees Salary valuesA:A:

Use CompanySelect DISTINCT SalaryFrom employee

Page 10: Sql server lab_3

Adavnced Database Programming 10

2. Simple Queries

WHERE Clause Form:

USE database_nameSELECT Column_List[ INTO New_table ]From table1[table2,….][ WHERE conditions ][ GROUP BY group_by_expression ][ HAVING search_conditions ][ ORDER BY order_expression[ASC|DESC] ]

Page 11: Sql server lab_3

Adavnced Database Programming 11

2. Simple Queries

Clauses must be written in order Comparison of strings are executed according to the

collation (Sort) order of the database ASCCI code Strings Comparisons will be done

character-to-character according to the difference between their values (Even if their lengths are different)

Priorities: NOT AND OR To avoid priorities conflicts, you may use parentheses

( )

Page 12: Sql server lab_3

Adavnced Database Programming 12

Notes to be Mentioned !

DISTINCT Clause Must precede all Columns list

Dates are stored in the table according to the system date format

Dates are viewed in the Query Result according to the equivalent date in Gregorian Calendar

To view them in Hijri Format Use:Select Convert(nchar(10),DateColumn,131)

Page 13: Sql server lab_3

Adavnced Database Programming 13

2. Simple Queries

Example :Q:Q: View all Male employees SSN and Department number who works in Departments located in Riyadh A:A:

Use CompanySelect SSN, DNumberFrom Employee, Departmentwhere Gender=‘M’ AND Dno=DNumber

and DLocation=‘Riyadh’

Page 14: Sql server lab_3

Adavnced Database Programming 14

2. Simple Queries

Example :

Q:Q: View all employees SSN who doesn’t work in Departments number 43A:A:

Use CompanySelect SSN

From Employeewhere NOT DNumber=43

Page 15: Sql server lab_3

Adavnced Database Programming 15

2. Simple Queries

Example :

Q:Q: View all employees SSN who doesn’t work in Departments number 43A:A:

Use CompanySelect SSN

From Employeewhere DNumber<>43

Page 16: Sql server lab_3

Adavnced Database Programming 16

2. Simple Queries

Example :

Q:Q: View all employees SSN who works in project Number 125 Or project number 326A:A:

Use CompanySelect ESSN

From Works_Onwhere PNO=125 OR PNO=326

Page 17: Sql server lab_3

Adavnced Database Programming 17

2. Simple Queries

Example :Q:Q: View all employees total salaryA:A:

Use CompanySelect SUM(Salary) Total_SalaryFrom Employee

Same way to use all other aggregate functions such as: MIN, MAX, AVG, COUNT ,COUNT_BIG

Page 18: Sql server lab_3

Adavnced Database Programming 18

2. Simple Queries

IN and BETWEEN Clauses :

USE database_nameSELECT Column_List

From table1[table2,….]WHERE Column_Name IN(value1, value2, value,…)

Page 19: Sql server lab_3

Adavnced Database Programming 19

2. Simple Queries

IN and BETWEEN Clauses :

USE database_nameSELECT Column_List

From table1[table2,….]WHERE Column_Name BETWEEN value1 AND value2

Page 20: Sql server lab_3

Adavnced Database Programming 20

2. Simple Queries

Example :

Q:Q: View all employees First & Last Name who their Salary is 20000 or 18500 or 16000

A:A:

Use CompanySelect FNAME, LNAME

From Employeewhere Salary IN (20000,18500,16000)

Page 21: Sql server lab_3

Adavnced Database Programming 21

2. Simple Queries

Example : (Equivalent Solution)Q:Q: View all employees First & Last Name who their Salary is 20000 or 18500 or 16000

A:A:Use CompanySelect FNAME, LNAMEFrom Employeewhere Salary=20000 OR Salary=18500

OR Salary=16000

Page 22: Sql server lab_3

Adavnced Database Programming 22

2. Simple Queries

Example :Q:Q: View all employees First & Last Name who their Salary is Not 20000 or 18500 or 16000

A:A:Use CompanySelect FNAME, LNAMEFrom Employeewhere Salary NOT IN (20000,18500,

16000)

Page 23: Sql server lab_3

Adavnced Database Programming 23

2. Simple Queries

Example :

Q:Q: View all employees SSN whose Salary is in range between 4526 and 4700

A:A:

Use CompanySelect SSN

From Employeewhere Salary BETWEEN 4526 AND 4700

Page 24: Sql server lab_3

Adavnced Database Programming 24

2. Simple Queries

Example : (Equivalent Solution)Q:Q: View all employees SSN whose Salary is in range between 4526 and 4700

A:A:Use CompanySelect SSNFrom Employeewhere Salary >= 4526 AND Salary <=

4700

Page 25: Sql server lab_3

Adavnced Database Programming 25

2. Simple Queries

Example :Q:Q: View all employees SSN whose BDate is NOT between 1/1/1960 and 4/3/1968

A:A:Use CompanySelect SSNFrom Employeewhere BDate NOT BETWEEN ‘1/1/1960’

AND ‘4/3/1968’

Page 26: Sql server lab_3

Adavnced Database Programming 26

Queries Involving NULL values

Comparisons with NULL Values will always return False

To RetrieveRetrieve rows with NULL values Use the Column_Name IS [NOT] NULL

operator in the WHERE Clause

Page 27: Sql server lab_3

Adavnced Database Programming 27

Queries Involving NULL values

Example :

Q:Q: View all Managers First & Last Name A:A:

Use CompanySelect FNAME, LNAME

From Employeewhere SUPERSSN IS NULL

Page 28: Sql server lab_3

Adavnced Database Programming 28

Queries Involving NULL values

Example :Q:Q: View all Employees SSN who are supervised by Mangers A:A:

Use CompanySelect SSNFrom Employeewhere SUPERSSN IS NOT NULL

[ Equivalent Sol. NOT (SUPERSSN IS NULL)]

Page 29: Sql server lab_3

Adavnced Database Programming 29

Queries Involving NULL values

Using ISNULL System Function :

SELECT ISNULL(ColumnName,Substitution Value) NewColumnName

FROM TableName NoteNote:

Substitution Value Must be same as column type

Page 30: Sql server lab_3

Adavnced Database Programming 30

Queries Involving NULL values

Example :Q:Q: View all Employees First Name who are supervised by Mangers(View their SuperSSN as 0)

A:A:Use CompanySelect FNAME, ISNULL(SuperSSN,0)

SupervidesdFrom Employee

Page 31: Sql server lab_3

Adavnced Database Programming 31

Like Operator

Compares column values with specified pattern which can be any character or DateTime data type

Column [NOT] LIKE ‘Pattern’ Pattern may be string or date constant or

expression by using Wild Characters such as: % any sequence of zero or more characters _ any single charcter

Page 32: Sql server lab_3

Adavnced Database Programming 32

Like Operator

Example :

Q:Q: View all Employees SSN who their First Name starts with O

A:A:

Use CompanySelect SSN

From Employeewhere FName LIKE ‘O%’

Page 33: Sql server lab_3

Adavnced Database Programming 33

Like Operator

Example :

Q:Q: View all Employees SSN who their Last Name second letter is a

A:A:

Use CompanySelect SSN

From Employeewhere LName LIKE ‘_a%’

Page 34: Sql server lab_3

Adavnced Database Programming 34

Like Operator: More Special Characters for Searching Patterns [ ],^

Example :

[[RangeStart--RangeEnd]] Range of Characters

[[1stChar2ndChar..]] List of Characters

^ ^ Negation of Range or List of Characters

Page 35: Sql server lab_3

Adavnced Database Programming 35

Like Operator: More Special Characters for Searching Patterns [ ],^

Example :Q:Q: View all department number who their location begin with a character in the range C through F

A:A:Use CompanySelect DNumberFrom Departmentwhere DLocation LIKE ‘[C-F]%’

Page 36: Sql server lab_3

Adavnced Database Programming 36

Like Operator: More Special Characters for Searching Patterns [ ],^

Example :

Q:Q: View all department number which their location do NOT begin with the letters J,K,L,M,N,O and whose Department Name doesn't begin with letters E Or Z

A:A:

Use CompanySelect DNumber

From Departmentwhere DLocation LIKE ‘[^J-O]%’ AND DName

LIKE ‘[^EZ]%’

Page 37: Sql server lab_3

Adavnced Database Programming 37

Like Operator

Example :Q:Q: View all Employees SSN who their Last Name doesn’t end with n

A:A:Use CompanySelect SSNFrom Employeewhere FName NOT LIKE ‘%n’

(NOT FName LIKE ‘%n’)

Page 38: Sql server lab_3

Adavnced Database Programming 38

Like Operator

Example :

Q:Q: How can i search for a String contains one of the Wild characters ( [],_, ^, %)

A:A:

By Using Square Brackets ‘[WildChar]’ OROR

By Using ESCAPE option Such as

Column_Name LIKE ‘ESCAPECHARWildChar’ ESCAPE ‘ESCAPECHAR’

Page 39: Sql server lab_3

Adavnced Database Programming 39

Like Operator: More Special Characters for Searching Patterns [ ],^

Example :Q:Q: View all department number who their location includes an Underscore

A:A:Use CompanySelect DNumberFrom Departmentwhere DLocation LIKE ‘%[ _ ]%’

OR DLocation LIKE ‘%!_% ESCAPE ‘!’