SlideShare une entreprise Scribd logo
1  sur  39
MS SQL Server 2005
  Lab # 3 :
      Practicing Queries




              Adavnced Database Programming   1
1. Practicing Company Database Example

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

                         Adavnced Database Programming           2
1. Practicing Company Database Example

       Create Department Table:
        DNAME: Department Name
        DNUMBER: Department Number (Primary Key)
        DLOCATION: Department Location
        MGRSSN: Manager Social Security Number
         (Foreign Key)
        MGRSTARTDATE: Manager Work Start Date




                     Adavnced Database Programming   3
1. Practicing Company Database Example

       Create Project Table:
        PNAME: Project Name
        PNUMBER: Project Number (Primary Key)
        PLOCATION: Project Location
        DNUM: Department Number which Project
         belongs to (Foreign Key)




                     Adavnced Database Programming   4
1. Practicing Company Database Example

       Create Works_ON Table:
        ESSN: Employee Social Security Number
         (Primary Key) + (Foreign Key)
        PNO: Project Number (Primary Key) + (Foreign
         Key)
        HOURS: Project Working Hours




                      Adavnced Database Programming     5
2. Simple Queries

   Basic SELECT Form:

    USE     database_name

    SELECT [ ALL | DISTINCT ] Column_List

    From    table1[table2,….]

    WHERE conditions
                 Adavnced Database Programming   6
2. Simple Queries
   Basic SELECT Form:

    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

                      Adavnced Database Programming    7
2. Simple Queries
Example :
 Q: View all Departments info details
 A:
     Use         Company
     Select      *
     From        department
OR
     Use     Company
     Select DNAME,DNUMBER,DLOCATION,
             MGRSSN,MGRSTARTDATE
     From department

               Adavnced Database Programming   8
2. Simple Queries
 Example :
Q: View all Employees Salary values
  A:
      Use         Company
      Select ALL Salary
      From        employee
Q: View Distinct Employees Salary values
  A:
      Use               Company
      Select DISTINCT Salary
      From              employee

                   Adavnced Database Programming   9
2. Simple Queries
   WHERE Clause 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] ]
                  Adavnced Database Programming   10
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
    ()

                      Adavnced Database Programming    11
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)
                   Adavnced Database Programming   12
2. Simple Queries
   Example :
    Q: View all Male employees SSN and
    Department number who works in Departments
    located in Riyadh
    A:
       Use         Company
       Select      SSN, DNumber
       From        Employee, Department
       where       Gender=‘M’ AND Dno=DNumber
                   and DLocation=‘Riyadh’
                  Adavnced Database Programming   13
2. Simple Queries

   Example :
    Q: View all employees SSN who doesn’t work in
    Departments number 43
    A:
       Use             Company
       Select          SSN
       From            Employee
       where NOT       DNumber=43

                  Adavnced Database Programming   14
2. Simple Queries

   Example :
    Q: View all employees SSN who doesn’t work in
    Departments number 43
    A:
       Use        Company
       Select     SSN
       From       Employee
       where      DNumber<>43

                  Adavnced Database Programming   15
2. Simple Queries

   Example :
    Q: View all employees SSN who works in
    project Number 125 Or project number 326
    A:
       Use        Company
       Select     ESSN
       From       Works_On
       where      PNO=125 OR PNO=326

                  Adavnced Database Programming   16
2. Simple Queries
   Example :
    Q: View all employees total salary
    A:
       Use        Company
       Select     SUM(Salary) Total_Salary
       From       Employee
   Same way to use all other aggregate functions
    such as: MIN, MAX, AVG, COUNT
    ,COUNT_BIG

                   Adavnced Database Programming   17
2. Simple Queries

   IN and BETWEEN Clauses :
    USE        database_name
    SELECT     Column_List
    From       table1[table2,….]
    WHERE      Column_Name IN(value1, value2,
    value,…)




                 Adavnced Database Programming   18
2. Simple Queries

   IN and BETWEEN Clauses :
    USE         database_name
    SELECT      Column_List
    From        table1[table2,….]
    WHERE       Column_Name BETWEEN
    value1 AND value2




               Adavnced Database Programming   19
2. Simple Queries

 Example :
  Q: View all employees First & Last Name who
  their Salary is 20000 or 18500 or 16000
A:
      Use         Company
      Select      FNAME, LNAME
      From        Employee
      where       Salary IN (20000,18500,16000)

                  Adavnced Database Programming   20
2. Simple Queries
 Example : (Equivalent Solution)
  Q: View all employees First & Last Name who
  their Salary is 20000 or 18500 or 16000
A:
      Use         Company
      Select      FNAME, LNAME
      From        Employee
      where       Salary=20000 OR Salary=18500
                  OR Salary=16000

                 Adavnced Database Programming   21
2. Simple Queries
 Example :
  Q: View all employees First & Last Name who
  their Salary is Not 20000 or 18500 or 16000
A:
      Use         Company
      Select      FNAME, LNAME
      From        Employee
      where       Salary NOT IN (20000,18500,
                  16000)

                 Adavnced Database Programming   22
2. Simple Queries

 Example :
  Q: View all employees SSN whose Salary is in
  range between 4526 and 4700
A:
     Use        Company
     Select     SSN
     From       Employee
     where      Salary BETWEEN 4526 AND 4700

                Adavnced Database Programming   23
2. Simple Queries
 Example : (Equivalent Solution)
  Q: View all employees SSN whose Salary is in
  range between 4526 and 4700
A:
     Use        Company
     Select     SSN
     From       Employee
     where      Salary >= 4526 AND Salary <=
  4700

                 Adavnced Database Programming   24
2. Simple Queries
 Example :
  Q: View all employees SSN whose BDate is
  NOT between 1/1/1960 and 4/3/1968
A:
     Use        Company
     Select     SSN
     From       Employee
     where      BDate NOT BETWEEN ‘1/1/1960’
                AND ‘4/3/1968’

                Adavnced Database Programming   25
Queries Involving NULL values

   Comparisons with NULL Values will always
    return False
   To Retrieve rows with NULL values Use the
        Column_Name IS [NOT] NULL
    operator in the WHERE Clause




                  Adavnced Database Programming   26
Queries Involving NULL values

   Example :
    Q: View all Managers First & Last Name
    A:
       Use        Company
       Select     FNAME, LNAME
       From       Employee
       where      SUPERSSN IS NULL



                   Adavnced Database Programming   27
Queries Involving NULL values
 Example :
  Q: View all Employees SSN who are supervised
  by Mangers
  A:
     Use        Company
     Select     SSN
     From       Employee
     where       SUPERSSN IS NOT NULL
[ Equivalent Sol.   NOT (SUPERSSN IS NULL)]

                Adavnced Database Programming   28
Queries Involving NULL values

   Using ISNULL System Function :

SELECT          ISNULL(ColumnName,
                Substitution Value) NewColumnName
    FROM        TableName
       Note:
        Note
           Substitution   Value Must be same as column
           type


                       Adavnced Database Programming   29
Queries Involving NULL values
 Example :
  Q: View all Employees First Name who are
  supervised by Mangers(View their SuperSSN as
  0)
A:
  Use     Company
  Select FNAME, ISNULL(SuperSSN,0)
          Supervidesd
  From    Employee

                 Adavnced Database Programming   30
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


                      Adavnced Database Programming   31
Like Operator

 Example :
  Q: View all Employees SSN who their First
  Name starts with O
A:
     Use        Company
     Select     SSN
     From       Employee
     where      FName LIKE ‘O%’

                 Adavnced Database Programming   32
Like Operator

 Example :
  Q: View all Employees SSN who their Last
  Name second letter is a
A:
     Use        Company
     Select     SSN
     From       Employee
     where      LName LIKE ‘_a%’

                 Adavnced Database Programming   33
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



                   Adavnced Database Programming          34
Like Operator: More Special Characters for
Searching Patterns [ ],^
 Example :
  Q: View all department number who their
  location begin with a character in the range C
  through F
A:
      Use       Company
      Select    DNumber
      From      Department
      where     DLocation LIKE ‘[C-F]%’

                   Adavnced Database Programming   35
Like Operator: More Special Characters for
Searching Patterns [ ],^
 Example :
  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:
      Use         Company
      Select      DNumber
      From        Department
      where       DLocation LIKE ‘[^J-O]%’ AND DName
                  LIKE ‘[^EZ]%’

                    Adavnced Database Programming   36
Like Operator
 Example :
  Q: View all Employees SSN who their Last
  Name doesn’t end with n
A:
     Use        Company
     Select     SSN
     From       Employee
     where      FName NOT LIKE ‘%n’
                (NOT FName LIKE ‘%n’)

                 Adavnced Database Programming   37
Like Operator

 Example :
  Q: How can i search for a String contains one of
  the Wild characters ( [],_, ^, %)
A:
  By Using Square Brackets ‘[WildChar]’ OR
  By Using ESCAPE option Such as
  Column_Name LIKE ‘ESCAPECHARWildChar’
  ESCAPE ‘ESCAPECHAR’

                  Adavnced Database Programming   38
Like Operator: More Special Characters for
Searching Patterns [ ],^
 Example :
  Q: View all department number who their
  location includes an Underscore
A:
     Use         Company
     Select      DNumber
     From        Department
     where       DLocation LIKE ‘%[ _ ]%’
  OR        DLocation LIKE ‘%!_% ESCAPE ‘!’

                 Adavnced Database Programming   39

Contenu connexe

Tendances

Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handouts
jhe04
 
HALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONSHALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONS
Talia Strnad
 
Using single row functions to customize output
Using single row functions to customize outputUsing single row functions to customize output
Using single row functions to customize output
Syed Zaid Irshad
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
Achmad Solichin
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
Vikas Gupta
 

Tendances (16)

Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handouts
 
Sql operator
Sql operatorSql operator
Sql operator
 
HALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONSHALL OF FAME INDUCTIONS
HALL OF FAME INDUCTIONS
 
Using single row functions to customize output
Using single row functions to customize outputUsing single row functions to customize output
Using single row functions to customize output
 
SQL
SQLSQL
SQL
 
SQL
SQLSQL
SQL
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
2 sql - single-row functions
2   sql - single-row functions2   sql - single-row functions
2 sql - single-row functions
 
Les03
Les03Les03
Les03
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
Sql.pptx
Sql.pptxSql.pptx
Sql.pptx
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
70433 Dumps DB
70433 Dumps DB70433 Dumps DB
70433 Dumps DB
 

En vedette

Ba skbk kertas keje
Ba skbk kertas kejeBa skbk kertas keje
Ba skbk kertas keje
Syafiq Syah
 
Znaczenie podziemnych magazynów gazu
Znaczenie podziemnych magazynów gazuZnaczenie podziemnych magazynów gazu
Znaczenie podziemnych magazynów gazu
Ku Ba
 

En vedette (18)

To all our special friends in mackay
To all our special friends in mackayTo all our special friends in mackay
To all our special friends in mackay
 
Transcription
TranscriptionTranscription
Transcription
 
Questionnaire
QuestionnaireQuestionnaire
Questionnaire
 
Musica
MusicaMusica
Musica
 
Advocating for the gifted learner
Advocating for the gifted learnerAdvocating for the gifted learner
Advocating for the gifted learner
 
MBI 1
MBI 1MBI 1
MBI 1
 
Daftar isi ukom
Daftar isi ukomDaftar isi ukom
Daftar isi ukom
 
Sql server lab_2
Sql server lab_2Sql server lab_2
Sql server lab_2
 
Dentif
DentifDentif
Dentif
 
Questionnaire
QuestionnaireQuestionnaire
Questionnaire
 
Ba skbk kertas keje
Ba skbk kertas kejeBa skbk kertas keje
Ba skbk kertas keje
 
Nak ibu ingin bicara
Nak ibu ingin bicaraNak ibu ingin bicara
Nak ibu ingin bicara
 
Photo Essay Zhao
Photo Essay ZhaoPhoto Essay Zhao
Photo Essay Zhao
 
Importance of living wills
Importance of living willsImportance of living wills
Importance of living wills
 
Znaczenie podziemnych magazynów gazu
Znaczenie podziemnych magazynów gazuZnaczenie podziemnych magazynów gazu
Znaczenie podziemnych magazynów gazu
 
Digital Marketing
Digital MarketingDigital Marketing
Digital Marketing
 
Proposal ukom
Proposal ukomProposal ukom
Proposal ukom
 
89567132 bab-2-sabuk
89567132 bab-2-sabuk89567132 bab-2-sabuk
89567132 bab-2-sabuk
 

Similaire à Sql server lab_3

lab14444444444444444444444444444444444444444
lab14444444444444444444444444444444444444444lab14444444444444444444444444444444444444444
lab14444444444444444444444444444444444444444
227567
 

Similaire à Sql server lab_3 (20)

Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
lab14444444444444444444444444444444444444444
lab14444444444444444444444444444444444444444lab14444444444444444444444444444444444444444
lab14444444444444444444444444444444444444444
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMS
 
DB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptxDB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptx
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Database queries
Database queriesDatabase queries
Database queries
 
UNIT-3.pptx
UNIT-3.pptxUNIT-3.pptx
UNIT-3.pptx
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
sql language
sql languagesql language
sql language
 
Nikhil Khandelwal BCA 3rd Year
Nikhil Khandelwal BCA 3rd YearNikhil Khandelwal BCA 3rd Year
Nikhil Khandelwal BCA 3rd Year
 
SQL
SQLSQL
SQL
 
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole CollegeDivyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
 
Simran kaur,BCA Final Year 2015
Simran kaur,BCA Final Year 2015Simran kaur,BCA Final Year 2015
Simran kaur,BCA Final Year 2015
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for Beginners
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
Chapter08
Chapter08Chapter08
Chapter08
 
01 basic orders
01   basic orders01   basic orders
01 basic orders
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptx
 

Sql server lab_3

  • 1. MS SQL Server 2005 Lab # 3 :  Practicing Queries Adavnced Database Programming 1
  • 2. 1. Practicing Company Database Example  Create Employee Table:  FNAME: First Name  LNAME: Last Name  SSN: Social Security Number (Primary Key)  BDATE: Birth Date  ADDRESS: Employee’s Address  Gender: F (Female) OR M (Male)  SALARY  SUPERSSN: Employee’s Supervisor SSN  DNO: No. of the Department the Emp. Works in (Foreign Key) Adavnced Database Programming 2
  • 3. 1. Practicing Company Database Example  Create Department Table:  DNAME: Department Name  DNUMBER: Department Number (Primary Key)  DLOCATION: Department Location  MGRSSN: Manager Social Security Number (Foreign Key)  MGRSTARTDATE: Manager Work Start Date Adavnced Database Programming 3
  • 4. 1. Practicing Company Database Example  Create Project Table:  PNAME: Project Name  PNUMBER: Project Number (Primary Key)  PLOCATION: Project Location  DNUM: Department Number which Project belongs to (Foreign Key) Adavnced Database Programming 4
  • 5. 1. Practicing Company Database Example  Create Works_ON Table:  ESSN: Employee Social Security Number (Primary Key) + (Foreign Key)  PNO: Project Number (Primary Key) + (Foreign Key)  HOURS: Project Working Hours Adavnced Database Programming 5
  • 6. 2. Simple Queries  Basic SELECT Form: USE database_name SELECT [ ALL | DISTINCT ] Column_List From table1[table2,….] WHERE conditions Adavnced Database Programming 6
  • 7. 2. Simple Queries  Basic SELECT Form: 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 Adavnced Database Programming 7
  • 8. 2. Simple Queries Example : Q: View all Departments info details A: Use Company Select * From department OR Use Company Select DNAME,DNUMBER,DLOCATION, MGRSSN,MGRSTARTDATE From department Adavnced Database Programming 8
  • 9. 2. Simple Queries  Example : Q: View all Employees Salary values A: Use Company Select ALL Salary From employee Q: View Distinct Employees Salary values A: Use Company Select DISTINCT Salary From employee Adavnced Database Programming 9
  • 10. 2. Simple Queries  WHERE Clause 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] ] Adavnced Database Programming 10
  • 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 () Adavnced Database Programming 11
  • 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) Adavnced Database Programming 12
  • 13. 2. Simple Queries  Example : Q: View all Male employees SSN and Department number who works in Departments located in Riyadh A: Use Company Select SSN, DNumber From Employee, Department where Gender=‘M’ AND Dno=DNumber and DLocation=‘Riyadh’ Adavnced Database Programming 13
  • 14. 2. Simple Queries  Example : Q: View all employees SSN who doesn’t work in Departments number 43 A: Use Company Select SSN From Employee where NOT DNumber=43 Adavnced Database Programming 14
  • 15. 2. Simple Queries  Example : Q: View all employees SSN who doesn’t work in Departments number 43 A: Use Company Select SSN From Employee where DNumber<>43 Adavnced Database Programming 15
  • 16. 2. Simple Queries  Example : Q: View all employees SSN who works in project Number 125 Or project number 326 A: Use Company Select ESSN From Works_On where PNO=125 OR PNO=326 Adavnced Database Programming 16
  • 17. 2. Simple Queries  Example : Q: View all employees total salary A: Use Company Select SUM(Salary) Total_Salary From Employee  Same way to use all other aggregate functions such as: MIN, MAX, AVG, COUNT ,COUNT_BIG Adavnced Database Programming 17
  • 18. 2. Simple Queries  IN and BETWEEN Clauses : USE database_name SELECT Column_List From table1[table2,….] WHERE Column_Name IN(value1, value2, value,…) Adavnced Database Programming 18
  • 19. 2. Simple Queries  IN and BETWEEN Clauses : USE database_name SELECT Column_List From table1[table2,….] WHERE Column_Name BETWEEN value1 AND value2 Adavnced Database Programming 19
  • 20. 2. Simple Queries  Example : Q: View all employees First & Last Name who their Salary is 20000 or 18500 or 16000 A: Use Company Select FNAME, LNAME From Employee where Salary IN (20000,18500,16000) Adavnced Database Programming 20
  • 21. 2. Simple Queries  Example : (Equivalent Solution) Q: View all employees First & Last Name who their Salary is 20000 or 18500 or 16000 A: Use Company Select FNAME, LNAME From Employee where Salary=20000 OR Salary=18500 OR Salary=16000 Adavnced Database Programming 21
  • 22. 2. Simple Queries  Example : Q: View all employees First & Last Name who their Salary is Not 20000 or 18500 or 16000 A: Use Company Select FNAME, LNAME From Employee where Salary NOT IN (20000,18500, 16000) Adavnced Database Programming 22
  • 23. 2. Simple Queries  Example : Q: View all employees SSN whose Salary is in range between 4526 and 4700 A: Use Company Select SSN From Employee where Salary BETWEEN 4526 AND 4700 Adavnced Database Programming 23
  • 24. 2. Simple Queries  Example : (Equivalent Solution) Q: View all employees SSN whose Salary is in range between 4526 and 4700 A: Use Company Select SSN From Employee where Salary >= 4526 AND Salary <= 4700 Adavnced Database Programming 24
  • 25. 2. Simple Queries  Example : Q: View all employees SSN whose BDate is NOT between 1/1/1960 and 4/3/1968 A: Use Company Select SSN From Employee where BDate NOT BETWEEN ‘1/1/1960’ AND ‘4/3/1968’ Adavnced Database Programming 25
  • 26. Queries Involving NULL values  Comparisons with NULL Values will always return False  To Retrieve rows with NULL values Use the Column_Name IS [NOT] NULL operator in the WHERE Clause Adavnced Database Programming 26
  • 27. Queries Involving NULL values  Example : Q: View all Managers First & Last Name A: Use Company Select FNAME, LNAME From Employee where SUPERSSN IS NULL Adavnced Database Programming 27
  • 28. Queries Involving NULL values  Example : Q: View all Employees SSN who are supervised by Mangers A: Use Company Select SSN From Employee where SUPERSSN IS NOT NULL [ Equivalent Sol. NOT (SUPERSSN IS NULL)] Adavnced Database Programming 28
  • 29. Queries Involving NULL values  Using ISNULL System Function : SELECT ISNULL(ColumnName, Substitution Value) NewColumnName FROM TableName  Note: Note  Substitution Value Must be same as column type Adavnced Database Programming 29
  • 30. Queries Involving NULL values  Example : Q: View all Employees First Name who are supervised by Mangers(View their SuperSSN as 0) A: Use Company Select FNAME, ISNULL(SuperSSN,0) Supervidesd From Employee Adavnced Database Programming 30
  • 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 Adavnced Database Programming 31
  • 32. Like Operator  Example : Q: View all Employees SSN who their First Name starts with O A: Use Company Select SSN From Employee where FName LIKE ‘O%’ Adavnced Database Programming 32
  • 33. Like Operator  Example : Q: View all Employees SSN who their Last Name second letter is a A: Use Company Select SSN From Employee where LName LIKE ‘_a%’ Adavnced Database Programming 33
  • 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 Adavnced Database Programming 34
  • 35. Like Operator: More Special Characters for Searching Patterns [ ],^  Example : Q: View all department number who their location begin with a character in the range C through F A: Use Company Select DNumber From Department where DLocation LIKE ‘[C-F]%’ Adavnced Database Programming 35
  • 36. Like Operator: More Special Characters for Searching Patterns [ ],^  Example : 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: Use Company Select DNumber From Department where DLocation LIKE ‘[^J-O]%’ AND DName LIKE ‘[^EZ]%’ Adavnced Database Programming 36
  • 37. Like Operator  Example : Q: View all Employees SSN who their Last Name doesn’t end with n A: Use Company Select SSN From Employee where FName NOT LIKE ‘%n’ (NOT FName LIKE ‘%n’) Adavnced Database Programming 37
  • 38. Like Operator  Example : Q: How can i search for a String contains one of the Wild characters ( [],_, ^, %) A: By Using Square Brackets ‘[WildChar]’ OR By Using ESCAPE option Such as Column_Name LIKE ‘ESCAPECHARWildChar’ ESCAPE ‘ESCAPECHAR’ Adavnced Database Programming 38
  • 39. Like Operator: More Special Characters for Searching Patterns [ ],^  Example : Q: View all department number who their location includes an Underscore A: Use Company Select DNumber From Department where DLocation LIKE ‘%[ _ ]%’ OR DLocation LIKE ‘%!_% ESCAPE ‘!’ Adavnced Database Programming 39