1 / 15

Chapter 3 Introduction to SQL(2)

Chapter 3 Introduction to SQL(2). 2 nd Semester, 2016 Sanghyun Park. Outline. History Data Definition Basic Query Structure Additional Basic Operations Set Operations Null Values Aggregate Functions Nested Subqueries Database Modification. Basic Query Structure.

markmesser
Download Presentation

Chapter 3 Introduction to SQL(2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3Introduction to SQL(2) 2nd Semester, 2016 Sanghyun Park

  2. Outline • History • Data Definition • Basic Query Structure • Additional Basic Operations • Set Operations • Null Values • Aggregate Functions • Nested Subqueries • Database Modification

  3. Basic Query Structure • A typical SQL query has the form:select A1, A2, ..., Anfrom r1, r2, ..., rmwhere P • Aisrepresent attributes • risrepresent relations • P is a predicate • The result of an SQL query is a relation

  4. Select Clause • Select clause lists the attributes desired in query result • Example: find the names of all instructorsselect namefrom instructor • Note: SQL names are case-insensitive(e.g., Name ≡ NAME ≡ name)

  5. Select Clause (Cont.) • SQL allows duplicates in relations and in query results • To force the elimination of duplicates, insert the keyword distinct after select • Find the department names of all instructors, and remove duplicatesselect distinct dept_namefrom instructor • The keyword all specifies that duplicates should not be removedselect all dept_namefrom instructor

  6. Select Clause (Cont.) • An asterisk in select clause denotes “all attributes”select *from instructor • Select clause can contain arithmetic expressionsselect ID, name, salary/12from instructor • Can rename arithmetic expressions using as clause select ID, name, salary/12 as monthly_salaryfrom instructor

  7. Where Clause • Where clause specifies conditions that the result must satisfy • To find all instructors in Comp. Sci. deptselect namefrom instructorwhere dept_name = ‘Comp. Sci.’ • Comparison results can be combined using the logical connectives and, or, and not select namefrom instructorwhere dept_name = ‘Comp. Sci.’ and salary > 80000

  8. From Clause • From clause lists the relations involved in the query • Find the Cartesian product: instructor X teachesselect *from instructor, teaches • The query above generates every possible instructor-teaches pair, with all attributes from both relations

  9. Cartesian Product

  10. Examples • Find the names of all instructors who have taught some course and the course_idselect name, course_idfrom instructor, teacheswhere instructor.ID = teaches.ID • Find the names of all instructors in the Art department who have taught some course and the course_idselect name, course_idfrom instructor, teacheswhere instructor.ID = teaches.ID and instructor.dept_name = ‘Art’

  11. Rename Operation • SQL allows renaming relations and attributes using the as clause: old-name as new-name • Find the names of all instructors who have a higher salary than some instructor in ‘Comp. Sci.’select distinct T.namefrom instructor as T, instructor as Swhere T.salary > S.salary and S.dept_name = ‘Comp. Sci.’ • Keyword as is optional and may be omitted: instructor as T ≡ instructor T

  12. Cartesian Product Example • Relation emp-super • Find the supervisor of ‘Bob’ • Find the supervisor of the supervisor of ‘Bob’ • Find ALL the supervisors (direct and indirect) of ‘Bob’ person supervisor Bob Alice Mary Susan Alice David David Mary

  13. String Operations • SQL includes a string-matching operator: like • The operator like uses patterns that are described using two special characters • Percent(%) matches any substring • Underscore(_) matches any character • Find the names of all instructors whose name includes the substring ‘dar’select namefrom instructorwhere name like ‘%dar%’

  14. Ordering the Display of Tuples • List in alphabetic order the names of all instructorsselect distinct namefrom instructororder by name • We may specify desc for descending order or asc for ascending order; ascending order is the default • Can sort on multiple attributes:order by dept_name, name

  15. Where Clause Predicates • SQL includes a between comparison operatorselect namefrom instructorwhere salary between 90000 and 100000 • Tuple comparisonselect name, course_idfrom instructor, teacheswhere (instructor.ID, dept_name) = (teaches.ID, ‘Biology’)

More Related