Trinity College

                       Basic SQL
                         Part 2
                           Timothy Richa...
Last Time
• SQL Language
 • Declarative language
• Many Standards
 • 1986-2008
• Data Definition Language (DDL)
 • CREATE T...
Today
• Objectives
   •   Ambiguous attribute names, Aliasing, Renaming, and Tuple Variables
  •    Unspecified WHERE and t...
Query Example
    For every project located in ‘Stafford’, list the
project number, the controlling department number,
and...
Attribute Ambiguity
In SQL, the same name can be used for two (or more)
   attributes as long as the attributes are in dif...
Attribute Ambiguity
In SQL, the same name can be used for two (or more)
   attributes as long as the attributes are in dif...
Attribute Ambiguity
In SQL, the same name can be used for two (or more)
   attributes as long as the attributes are in dif...
Attribute Ambiguity
       Imagine the Dno and Lname attributes
        of the EMPLOYEE relation were called
    Dnumber a...
Attribute Ambiguity
       Imagine the Dno and Lname attributes
        of the EMPLOYEE relation were called
    Dnumber a...
Attribute Ambiguity
       Imagine the Dno and Lname attributes
        of the EMPLOYEE relation were called
    Dnumber a...
Attribute Ambiguity
            Imagine the Dno and Lname attributes
             of the EMPLOYEE relation were called
   ...
Attribute Ambiguity
         You can also qualify attribute names for clarity...



SELECT      EMPLOYEE.Fname, EMPLOYEE.N...
Attribute Ambiguity
         You can also qualify attribute names for clarity...
                   This is quite verbose!...
Attribute Ambiguity
         You can also qualify attribute names for clarity...
                   This is quite verbose!...
Attribute Ambiguity
         You can also qualify attribute names for clarity...
                   This is quite verbose!...
Attribute Ambiguity


SELECT      E.Fname, E.Lname, S.Fname, S.Lname
FROM        EMPLOYEE AS E, EMPLOYEE AS S
WHERE       ...
Attribute Ambiguity
        What does this query mean in English?


SELECT      E.Fname, E.Lname, S.Fname, S.Lname
FROM   ...
Attribute Ambiguity
      What does this query mean in English?


SELECT     E.Fname, E.Lname, S.Fname, S.Lname
FROM      ...
Attribute Ambiguity
      What does this query mean in English?


SELECT     E.Fname, E.Lname, S.Fname, S.Lname
FROM      ...
Attribute Ambiguity
           It is also possible to rename all attributes


EMPLOYEE AS E(Fn,Mi,Ln,Ssn,Bd,Addr,Sx,Sal,Ss...
Missing WHERE
    What if I do not want to impose a condition
           on the tuples that are returned?




Trinity Coll...
Missing WHERE
    What if I do not want to impose a condition
           on the tuples that are returned?



   Turns out ...
Missing WHERE
            What if I do not want to impose a condition
                   on the tuples that are returned?
...
Missing WHERE



                  Because we do not specify a “join condition” between
                     the two table...
Missing WHERE
         SSN
E      02783465
       02537596

       Dname
D      Research
     Administration

            ...
Missing WHERE
         SSN
E      02783465                                               SSN                Dname
       0...
Missing WHERE
         SSN
E      02783465                                               SSN                Dname
       0...
Missing WHERE
         SSN
E      02783465
                                                              SSN              ...
Selecting *
          Sometimes we want all the attributes
                 returned in a result.


                  SELE...
Selecting *
          Sometimes we want all the attributes
                 returned in a result.


                  SELE...
Tables as Sets
            SQL typically treats a table as a set.




Trinity College, Hartford CT • Department of Compute...
Tables as Sets
            SQL typically treats a table as a set.

              More accurately, as a multiset.
         ...
Tables as Sets
            SQL typically treats a table as a set.

              More accurately, as a multiset.
         ...
Tables as Sets
            SQL typically treats a table as a set.

              More accurately, as a multiset.
         ...
Tables as Sets
            SQL typically treats a table as a set.

              More accurately, as a multiset.
         ...
Tables as Sets
• SQL allows duplicates because...
  • Duplicate elimination is an expensive operation.
                  C...
Tables as Sets

          Retrieve the salary of every employee


                  SELECT          Salary
               ...
Tables as Sets

          Retrieve the salary of every employee


                  SELECT          Salary
               ...
Tables as Sets

          Retrieve the salary of every employee


                  SELECT          Salary
               ...
Tables as Sets

          Retrieve the salary of every employee


                  SELECT          DISTINCT Salary
      ...
Tables as Sets

                 Retrieve the salary of every employee

We can use
                    SELECT             ...
Tables as Sets
SQL has direct support for some set operations

        Make a list of all project numbers for projects
   ...
Tables as Sets
SQL has direct support for some set operations

        Make a list of all project numbers for projects
   ...
Tables as Sets
SQL has direct support for some set operations

        Make a list of all project numbers for projects
   ...
Tables as Sets
SQL has direct support for some set operations

        Make a list of all project numbers for projects
   ...
Tables as Sets
SQL has direct support for some set operations

          In addition to UNION SQL also supports
          ...
Substring Pattern Matching
  Retrieve all employees whose address is in Houston,Texas.


  SELECT         Fname, Lname
  F...
Substring Pattern Matching
    Find all employees who were born during the 1950s.


  SELECT         Fname, Lname
  FROM  ...
Arithmetic Operations
    Show the resulting salaries if every employee working on
       the ‘ProductX’ project is given ...
Next time...

               Ordering Query Results
                               AND
    Insert, Deleting, and Updating,...
Upcoming SlideShare
Loading in...5
×

Basic SQL Part 2

1,281

Published on

Basic SQL Part 2

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,281
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
72
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide


















































  • Basic SQL Part 2

    1. 1. Trinity College Basic SQL Part 2 Timothy Richards Trinity College, Hartford CT • Department of Computer Science • CPSC 372
    2. 2. Last Time • SQL Language • Declarative language • Many Standards • 1986-2008 • Data Definition Language (DDL) • CREATE TABLE • Data Manipulation Language (DML) • SELECT-FROM-WHERE Trinity College, Hartford CT • Department of Computer Science • CPSC 372 2
    3. 3. Today • Objectives • Ambiguous attribute names, Aliasing, Renaming, and Tuple Variables • Unspecified WHERE and the use of * • Tables as sets • Pattern matching and arithmetic operators • Ordering query results • INSERT, DELETE, UPDATE • Goal After today’s class you should be able to create a relational database schema/instance using SQL DDL, manipulate data in the tables you create, and define queries on that data. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 3
    4. 4. Query Example For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date. SELECT Pnumber, Dnum, Lname, Address, Bdate FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Plocation=‘Stafford’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 4
    5. 5. Attribute Ambiguity In SQL, the same name can be used for two (or more) attributes as long as the attributes are in different relations. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 5
    6. 6. Attribute Ambiguity In SQL, the same name can be used for two (or more) attributes as long as the attributes are in different relations. Ambiguous Trinity College, Hartford CT • Department of Computer Science • CPSC 372 6
    7. 7. Attribute Ambiguity In SQL, the same name can be used for two (or more) attributes as long as the attributes are in different relations. Ambiguous Solution: qualify attributes names with the relation names to avoid ambiguity Trinity College, Hartford CT • Department of Computer Science • CPSC 372 7
    8. 8. Attribute Ambiguity Imagine the Dno and Lname attributes of the EMPLOYEE relation were called Dnumber and Name and the Dname attribute of DEPARTMENT was called Name. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 8
    9. 9. Attribute Ambiguity Imagine the Dno and Lname attributes of the EMPLOYEE relation were called Dnumber and Name and the Dname attribute of DEPARTMENT was called Name. List the first name, last name, and address of all employees in the Research department. SELECT Fname, Name, Address FROM EMPLOYEE, DEPARTMENT WHERE Name=‘Research’ AND Dnumber=Dnumber; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 9
    10. 10. Attribute Ambiguity Imagine the Dno and Lname attributes of the EMPLOYEE relation were called Dnumber and Name and the Dname attribute of DEPARTMENT was called Name. List the first name, last name, and address of all employees in the Research department. SELECT Fname, Name, Address FROM EMPLOYEE, DEPARTMENT WHERE Name=‘Research’ AND Ambiguity Dnumber=Dnumber; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 10
    11. 11. Attribute Ambiguity Imagine the Dno and Lname attributes of the EMPLOYEE relation were called Dnumber and Name and the Dname attribute of DEPARTMENT was called Name. List the first name, last name, and address of all employees in the Research department. Qualify! SELECT Fname, EMPLOYEE.Name, Address FROM EMPLOYEE, DEPARTMENT WHERE DEPARTMENT.Name=‘Research’ AND DEPARTMENT.Dnumber=EMPLOYEE.Dnumber; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 11
    12. 12. Attribute Ambiguity You can also qualify attribute names for clarity... SELECT EMPLOYEE.Fname, EMPLOYEE.Name, EMPLOYEE.Address FROM EMPLOYEE, DEPARTMENT WHERE DEPARTMENT.Name=‘Research’ AND DEPARTMENT.Dnumber=EMPLOYEE.Dnumber; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 12
    13. 13. Attribute Ambiguity You can also qualify attribute names for clarity... This is quite verbose! SELECT EMPLOYEE.Fname, EMPLOYEE.Name, EMPLOYEE.Address FROM EMPLOYEE, DEPARTMENT WHERE DEPARTMENT.Name=‘Research’ AND DEPARTMENT.Dnumber=EMPLOYEE.Dnumber; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 13
    14. 14. Attribute Ambiguity You can also qualify attribute names for clarity... This is quite verbose! SELECT E.Fname, E.Name, E.Address FROM EMPLOYEE AS E, DEPARTMENT AS D WHERE D.Name=‘Research’ AND D.Dnumber=E.Dnumber; For this we can use table aliases! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 14
    15. 15. Attribute Ambiguity You can also qualify attribute names for clarity... This is quite verbose! SELECT E.Fname, E.Name, E.Address FROM EMPLOYEE AS E, DEPARTMENT AS D WHERE D.Name=‘Research’ AND D.Dnumber=E.Dnumber; Although this is nice for reducing typing... it is required for queries that refer to the same relation twice! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 15
    16. 16. Attribute Ambiguity SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.Super_ssn=S.Ssn; Although this is nice for reducing typing... it is required for queries that refer to the same relation twice! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 16
    17. 17. Attribute Ambiguity What does this query mean in English? SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.Super_ssn=S.Ssn; Although this is nice for reducing typing... it is required for queries that refer to the same relation twice! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 17
    18. 18. Attribute Ambiguity What does this query mean in English? SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.Super_ssn=S.Ssn; For each employee, retrieve the employee’s first and last name and the first and last name of his or her immediate supervisor. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 18
    19. 19. Attribute Ambiguity What does this query mean in English? SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.Super_ssn=S.Ssn; Tuple Variables For each employee, retrieve the employee’s first and last name and the first and last name of his or her immediate supervisor. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 19
    20. 20. Attribute Ambiguity It is also possible to rename all attributes EMPLOYEE AS E(Fn,Mi,Ln,Ssn,Bd,Addr,Sx,Sal,Sssn,Dno) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 20
    21. 21. Missing WHERE What if I do not want to impose a condition on the tuples that are returned? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 21
    22. 22. Missing WHERE What if I do not want to impose a condition on the tuples that are returned? Turns out that you do not need a WHERE clause! SELECT Ssn FROM EMPLOYEE; SELECT Ssn, Dname FROM EMPLOYEE, DEPARTMENT; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 22
    23. 23. Missing WHERE What if I do not want to impose a condition on the tuples that are returned? Turns out that you do not need a WHERE clause! SELECT Ssn FROM EMPLOYEE; What tuples does SELECT Ssn, Dname this return? FROM EMPLOYEE, DEPARTMENT; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 23
    24. 24. Missing WHERE Because we do not specify a “join condition” between the two tables, we get the cross product of the specified attributes between the two tables! What tuples does SELECT Ssn, Dname this return? FROM EMPLOYEE, DEPARTMENT; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 24
    25. 25. Missing WHERE SSN E 02783465 02537596 Dname D Research Administration Because we do not specify a “join condition” between the two tables, we get the cross product of the specified attributes between the two tables! What tuples does SELECT Ssn, Dname this return? FROM EMPLOYEE, DEPARTMENT; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 25
    26. 26. Missing WHERE SSN E 02783465 SSN Dname 02537596 02783465 Research * 02537596 Research Dname 02783465 Administration D Research 02537596 Administration Administration Because we do not specify a “join condition” between the two tables, we get the cross product of the specified attributes between the two tables! What tuples does SELECT Ssn, Dname this return? FROM EMPLOYEE, DEPARTMENT; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 26
    27. 27. Missing WHERE SSN E 02783465 SSN Dname 02537596 02783465 Research * 02537596 Research Dname 02783465 Administration D Research 02537596 Administration Administration We can get sensible results if we include a join condition on the SSN attribute. What tuples does SELECT Ssn, Dname this return? FROM EMPLOYEE, DEPARTMENT; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 27
    28. 28. Missing WHERE SSN E 02783465 SSN Dname 02537596 * 02783465 Research Dname 02537596 Administration D Research Administration We can get sensible results if we include a join condition on the SSN attribute. What tuples does SELECT Ssn, Dname this return? FROM EMPLOYEE, DEPARTMENT WHERE Dno=Dnumber; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 28
    29. 29. Selecting * Sometimes we want all the attributes returned in a result. SELECT * FROM EMPLOYEE; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 29
    30. 30. Selecting * Sometimes we want all the attributes returned in a result. SELECT * FROM EMPLOYEE; This will return each value for every attribute in a tuple. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 30
    31. 31. Tables as Sets SQL typically treats a table as a set. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 31
    32. 32. Tables as Sets SQL typically treats a table as a set. More accurately, as a multiset. What is a multiset? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 32
    33. 33. Tables as Sets SQL typically treats a table as a set. More accurately, as a multiset. What is a multiset? A set, where duplicate tuples can appear more than once in a table. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 33
    34. 34. Tables as Sets SQL typically treats a table as a set. More accurately, as a multiset. What is a multiset? A set, where duplicate tuples can appear more than once in a table. But, I thought that was bad?! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 34
    35. 35. Tables as Sets SQL typically treats a table as a set. More accurately, as a multiset. What is a multiset? A set, where duplicate tuples can appear more than once in a table. But, I thought that was bad?! Turns out, in practice this is useful... Trinity College, Hartford CT • Department of Computer Science • CPSC 372 35
    36. 36. Tables as Sets • SQL allows duplicates because... • Duplicate elimination is an expensive operation. Can you think why? • The user may want to see duplicate tuples in the result of a query • When an aggregate function is applied to tuples, in most cases we do not want to eliminate duplicates. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 36
    37. 37. Tables as Sets Retrieve the salary of every employee SELECT Salary FROM EMPLOYEE; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 37
    38. 38. Tables as Sets Retrieve the salary of every employee SELECT Salary FROM EMPLOYEE; Ok, this returns tuples containing the salary of each employee. Is it useful? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 38
    39. 39. Tables as Sets Retrieve the salary of every employee SELECT Salary FROM EMPLOYEE; Ok, this returns tuples containing the salary of each employee. Is it useful? What if we only wanted to see the distinct salaries? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 39
    40. 40. Tables as Sets Retrieve the salary of every employee SELECT DISTINCT Salary FROM EMPLOYEE; Ok, this returns tuples containing the salary of each employee. Is it useful? What if we only wanted to see the distinct salaries? This results in distinct salaries! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 40
    41. 41. Tables as Sets Retrieve the salary of every employee We can use SELECT ALL Salary ALL to retrieve all ---same as default. FROM EMPLOYEE; Ok, this returns tuples containing the salary of each employee. Is it useful? What if we only wanted to see the distinct salaries? This results in distinct salaries! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 41
    42. 42. Tables as Sets SQL has direct support for some set operations Make a list of all project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as a manager of the department that controls the project. If SQL supports set union (it does), how might we want to compose this query? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 42
    43. 43. Tables as Sets SQL has direct support for some set operations Make a list of all project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as a manager of the department that controls the project. SELECT DISTINCT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE First write the query WHERE Dnum=Dnumber AND Mgr_ssn=Ssn for the first part... AND Lname=‘Smith’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 43
    44. 44. Tables as Sets SQL has direct support for some set operations Make a list of all project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as a manager of the department that controls the project. SELECT DISTINCT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Lname=‘Smith’; Then write a query for SELECT DISTINCT Pnumber the second part... FROM PROJECT, WORKS_ON, EMPLOYEE WHERE Pnumber=Pno AND Essn=Ssn AND Lname=‘Smith’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 44
    45. 45. Tables as Sets SQL has direct support for some set operations Make a list of all project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as a manager of the department that controls the project. (SELECT DISTINCT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Lname=‘Smith’) Then union the UNION results together! (SELECT DISTINCT Pnumber FROM PROJECT, WORKS_ON, EMPLOYEE WHERE Pnumber=Pno AND Essn=Ssn AND Lname=‘Smith’); Trinity College, Hartford CT • Department of Computer Science • CPSC 372 45
    46. 46. Tables as Sets SQL has direct support for some set operations In addition to UNION SQL also supports set difference (EXCEPT) and set intersection (INTERSECT) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 46
    47. 47. Substring Pattern Matching Retrieve all employees whose address is in Houston,Texas. SELECT Fname, Lname FROM EMPLOYEE WHERE Address LIKE ‘%Houston,TX%’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 47
    48. 48. Substring Pattern Matching Find all employees who were born during the 1950s. SELECT Fname, Lname FROM EMPLOYEE WHERE Bdate LIKE ‘__5_______’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 48
    49. 49. Arithmetic Operations Show the resulting salaries if every employee working on the ‘ProductX’ project is given a 10 percent raise. SELECT E.Fname, E.Lname, E.Salary*1.1 AS incr FROM EMPLOYEE AS E, WORKS_ON AS W, PROJECT AS P WHERE E.Ssn=W.Essn AND W.Pno=P.Pnumber AND P.Pname=‘ProductX’ Trinity College, Hartford CT • Department of Computer Science • CPSC 372 49
    50. 50. Next time... Ordering Query Results AND Insert, Deleting, and Updating, Oh My! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 50
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×