SQL Server Lab 3

download SQL Server Lab 3

of 39

Transcript of SQL Server Lab 3

  • 8/8/2019 SQL Server Lab 3

    1/39

    Adavnced Database Programming 1

    MS SQL Server 2005MS SQL Server 2005

    Lab # 3 : Practicing Queries

  • 8/8/2019 SQL Server Lab 3

    2/39

    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: Employees Address

    Gender:Gender: F (Female) OR M (Male)

    SALARYSALARY SUPERSSN:SUPERSSN: Employees SupervisorSSN

    DNO:DNO: No.ofthe Department the Emp. Works in (Foreign(Foreign

    Key)Key)

  • 8/8/2019 SQL Server Lab 3

    3/39

    Adavnced Database Programming 3

    1. Practicing Company Database Example

    Create DepartmentDepartmentTable:

    DNAME:DNAME: Department Name

    DNUMBER:DNUMBER: Department Number(Primary Key)

    DLOCATION:DLOCATION: Department Location

    MGRSSN:MGRSSN: ManagerSocial Security Number

    (Foreign Key)(Foreign Key)

    MGRSTARTDATE:MGRSTARTDATE: Manager Work Start Date

  • 8/8/2019 SQL Server Lab 3

    4/39

    Adavnced Database Programming 4

    1. Practicing Company Database Example

    Create ProjectProjectTable:

    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)

  • 8/8/2019 SQL Server Lab 3

    5/39

    Adavnced Database Programming 5

    1. Practicing Company Database Example

    Create Works_ONWorks_ONTable:

    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

  • 8/8/2019 SQL Server Lab 3

    6/39

    Adavnced Database Programming 6

    2. Simple Queries

    Basic SELECTForm:

    USE

    d

    atabase_name

    SELECT [ ALL | DISTINCT ] Column_List

    From table1[table2,.]

    WHERE conditions

  • 8/8/2019 SQL Server Lab 3

    7/39

    Adavnced Database Programming 7

    2. Simple Queries

    Basic SELECTForm:

    Column_ListColumn_ListSpecifications:: * denotes for all columns ofall tables

    specified in the FROM Clause

    Specified Column name

    Column_Name as Column_Heading To Replace

    OR to assign a new column name to anexpression

    An Expression, System or Aggregate Function

  • 8/8/2019 SQL Server Lab 3

    8/39

    Adavnced Database Programming 8

    2. Simple Queries

    Example :

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

    Use CompanySelect *

    From department

    OR

    Use CompanySelect DNAME,DNUMBER,DLOCATION,MGRSSN,MGRSTARTDATE

    From department

  • 8/8/2019 SQL Server Lab 3

    9/39

    Adavnced Database Programming 9

    2. Simple Queries

    Example :

    Q:Q: View all Employees Salary valuesA:A:

    Use CompanySelect ALL Salary

    From employee

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

    Use CompanySelect DISTINCT Salary

    From employee

  • 8/8/2019 SQL Server Lab 3

    10/39

    Adavnced Database Programming 10

    2. Simple Queries

    WHEREClause Form:

    USE database_name

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

  • 8/8/2019 SQL Server Lab 3

    11/39

    Adavnced Database Programming 11

    2. Simple Queries

    Clauses must be written in order

    Comparison ofstrings are executed according to the

    collation (Sort) orderof the database

    ASCCI code Strings Comparisons will be donecharacter-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

    ( )

  • 8/8/2019 SQL Server Lab 3

    12/39

    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)

  • 8/8/2019 SQL Server Lab 3

    13/39

    Adavnced Database Programming 13

    2. Simple Queries

    Example :

    Q:Q: View all Male employees SSN andDepartment number who works in Departments

    located in RiyadhA:A:

    Use CompanySelect SSN, DNumber

    From Employee, Departmentwhere Gender=M AND Dno=DNumber

    and DLocation=Riyadh

  • 8/8/2019 SQL Server Lab 3

    14/39

    Adavnced Database Programming 14

    2. Simple Queries

    Example :

    Q:Q: View all employees SSN who doesnt work in

    Departments number 43

    A:A:

    Use Company

    Select SSN

    From Employeewhere NOT DNumber=43

  • 8/8/2019 SQL Server Lab 3

    15/39

    Adavnced Database Programming 15

    2. Simple Queries

    Example :

    Q:Q: View all employees SSN who doesnt work in

    Departments number 43

    A:A:

    Use Company

    Select SSN

    From Employeewhere DNumber43

  • 8/8/2019 SQL Server Lab 3

    16/39

    Adavnced Database Programming 16

    2. Simple Queries

    Example :

    Q:Q: View all employees SSN who works in

    project Number 125 Or project number 326

    A:A:

    Use Company

    Select ESSN

    From Works_Onwhere PNO=125 OR PNO=326

  • 8/8/2019 SQL Server Lab 3

    17/39

    Adavnced Database Programming 17

    2. Simple Queries

    Example :

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

    Use CompanySelect SUM(Salary) Total_Salary

    From Employee

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

  • 8/8/2019 SQL Server Lab 3

    18/39

    Adavnced Database Programming 18

    2. Simple Queries

    IN and BETWEEN Clauses :

    USE database_name

    SELECT Column_List

    From table1[table2,.]

    WHERE Column_Name IN(value1, value2,

    value,)

  • 8/8/2019 SQL Server Lab 3

    19/39

    Adavnced Database Programming 19

    2. Simple Queries

    IN and BETWEEN Clauses :

    USE database_name

    SELECT Column_List

    From table1[table2,.]

    WHERE Column_Name BETWEEN value1

    AND value2

  • 8/8/2019 SQL Server Lab 3

    20/39

    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 Company

    Select FNAME, LNAME

    From Employee

    where Salary IN (20000,18500,16000)

  • 8/8/2019 SQL Server Lab 3

    21/39

    Adavnced Database Programming 21

    2. Simple Queries

    Example : (Equivalent Solution)

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

    A:A:

    Use CompanySelect FNAME, LNAME

    FromE

    mployeewhere Salary=20000 OR Salary=18500OR Salary=16000

  • 8/8/2019 SQL Server Lab 3

    22/39

    Adavnced Database Programming 22

    2. Simple Queries

    Example :

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

    A:A:

    Use CompanySelect FNAME, LNAME

    FromE

    mployeewhere Salary NOT IN (20000,18500,16000)

  • 8/8/2019 SQL Server Lab 3

    23/39

    Adavnced Database Programming 23

    2. Simple Queries

    Example :

    Q:Q: View all employees SSN whose Salary is in

    range between 4526 and4700

    A:A:

    Use Company

    Select SSN

    From Employee

    where Salary BETWEEN 4526 AND 4700

  • 8/8/2019 SQL Server Lab 3

    24/39

    Adavnced Database Programming 24

    2. Simple Queries

    Example : (Equivalent Solution)

    Q:Q: View all employees SSN whose Salary is inrange between 4526 and4700

    A:A:

    Use CompanySelect SSN

    FromE

    mployeewhere Salary >= 4526 AND Salary

  • 8/8/2019 SQL Server Lab 3

    25/39

    Adavnced Database Programming 25

    2. Simple Queries

    Example :

    Q:Q: View all employees SSN whose BDate isNOT between 1/1/1960 and4/3/1968

    A:A:

    Use CompanySelect SSN

    FromE

    mployeewhere BDate NOT BETWEEN 1/1/1960AND 4/3/1968

  • 8/8/2019 SQL Server Lab 3

    26/39

    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 WHEREClause

  • 8/8/2019 SQL Server Lab 3

    27/39

    Adavnced Database Programming 27

    Queries Involving NULL values

    Example :

    Q:Q: View all Managers First & Last Name

    A:A:

    Use Company

    Select FNAME, LNAME

    From Employee

    where SUPERSSN IS NULL

  • 8/8/2019 SQL Server Lab 3

    28/39

    Adavnced Database Programming 28

    Queries Involving NULL values

    Example :

    Q:Q: View all Employees SSN who are supervisedby Mangers

    A:A:Use CompanySelect SSN

    From Employeewhere SUPERSSN IS NOT NULL

    [ Equivalent Sol. NOT ( SUPERSSN IS NULL)]

  • 8/8/2019 SQL Server Lab 3

    29/39

    Adavnced Database Programming 29

    Queries Involving NULL values

    UsingISNULL System Function :

    SELECT ISNULL(ColumnName,

    Substitution Value)NewColumnName

    FROM TableName

    NoteNote:

    Substitution Value Must be same as columntype

  • 8/8/2019 SQL Server Lab 3

    30/39

    Adavnced Database Programming 30

    Queries Involving NULL values

    Example :

    Q:Q: View all Employees First Name who aresupervisedby Mangers(View their SuperSSN as

    0)A:A:

    Use CompanySelect FNAME, ISNULL(SuperSSN,0)

    Supervidesd

    From Employee

  • 8/8/2019 SQL Server Lab 3

    31/39

    Adavnced Database Programming 31

    Like Operator

    Compares column values with specified

    pattern which can be any characteror

    DateTime data type

    Column [NOT] LIKE Pattern

    Pattern may be string or date constant or

    expression by using Wild Characters such as:

    % any sequence ofzeroor more characters _ any single charcter

  • 8/8/2019 SQL Server Lab 3

    32/39

    Adavnced Database Programming 32

    Like Operator

    Example :

    Q:Q: View all Employees SSN who their First

    Name starts with O

    A:A:

    Use Company

    Select SSN

    From Employee

    where FName LIKE O%

  • 8/8/2019 SQL Server Lab 3

    33/39

    Adavnced Database Programming 33

    Like Operator

    Example :

    Q:Q: View all Employees SSN who their Last Name

    second letter is a

    A:A:

    Use Company

    Select SSN

    From Employee

    where LName LIKE _a%

  • 8/8/2019 SQL Server Lab 3

    34/39

    Adavnced Database Programming 34

    Like Operator:More Special Characters for

    Searching Patterns [ ],^

    Example :

    [[RangeStart--RangeEnd]] Range ofCharacters

    [[1stChar2ndChar..]] List ofCharacters

    ^^

    Negation ofRange orList ofCharacters

  • 8/8/2019 SQL Server Lab 3

    35/39

    Adavnced Database Programming 35

    Like Operator:More Special Characters for

    Searching Patterns [ ],^

    Example :

    Q:Q: View alldepartment number who theirlocation begin with a character in the range C

    through FA:A:

    Use CompanySelect DNumber

    From Departmentwhere DLocation LIKE [C-F]%

  • 8/8/2019 SQL Server Lab 3

    36/39

    Adavnced Database Programming 36

    Like Operator:More Special Characters for

    Searching Patterns [ ],^

    Example :

    Q:Q: View alldepartment number which their location do

    NOT begin with the letters J,K,L,M,N,O andwhose

    Department Name doesn't begin with letters E Or ZA:A:

    Use Company

    Select DNumber

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

    LIKE [^EZ]%

  • 8/8/2019 SQL Server Lab 3

    37/39

    Adavnced Database Programming 37

    Like Operator

    Example :

    Q:Q: View all Employees SSN who their Last Namedoesnt endwith n

    A:A:

    Use CompanySelect SSN

    FromE

    mployeewhere FName NOT LIKE %n

    (NOT FName LIKE %n)

  • 8/8/2019 SQL Server Lab 3

    38/39

    Adavnced Database Programming 38

    Like Operator

    Example :

    Q:Q: How can i search for a String contains one of

    the Wildcharacters ( [],_, ^, %)

    A:A:

    By Using Square Brackets [WildChar] OROR

    By Using ESCAPEoption Such as

    Column_Name LIKE ESCAPECHARWildChar

    ESCAPE ESCAPECHAR

  • 8/8/2019 SQL Server Lab 3

    39/39

    Adavnced Database Programming 39

    Like Operator:More Special Characters for

    Searching Patterns [ ],^

    Example :

    Q:Q: View alldepartment number who theirlocation includes an Underscore

    A:A:

    Use CompanySelect DNumber

    From Departmentwhere DLocation LIKE %[ _ ]%

    OR DLocation LIKE %!_% ESCAPE !