Lecture 4 - Introduction to SQL PDF

Summary

This lecture provides an introduction to SQL, covering basic query structure, additional operations, set operations, null values, aggregate functions, nested subqueries, and modification of a database. The lecture also includes examples of queries and details to highlight operations.

Full Transcript

Lecture 4 Chapter 3: Introduction to SQL Basic Query Structure Additional Basic Operations Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database Drop and Alter Table Constructs drop table student Deletes the table an...

Lecture 4 Chapter 3: Introduction to SQL Basic Query Structure Additional Basic Operations Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database Drop and Alter Table Constructs drop table student Deletes the table and its contents delete from student Deletes all contents of table, but retains table alter table alter table r add A D where A is the name of the attribute to be added to relation r and D is the domain of A. All tuples in the relation are assigned null as the value for the new attribute. alter table r drop A where A is the name of an attribute of relation r Dropping of attributes not supported by many databases Basic Query Structure The SQL data-manipulation language (DML) provides the ability to query information, and insert, delete and update tuples A typical SQL query has the form: select A1, A2,..., An from r1, r2,..., rm where P Ai represents an attribute Ri represents a relation P is a predicate. The result of an SQL query is a relation. The select Clause The select clause list the attributes desired in the result of a query corresponds to the projection operation of the relational algebra Example: find the names of all instructors: select name from instructor NOTE: SQL names are case insensitive (i.e., you may use upper- or lower-case letters.) E.g. Name ≡ NAME ≡ name Some people use upper case wherever we use bold font. The select Clause (Cont.) SQL allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. Find the names of all departments with instructor, and remove duplicates select distinct dept_name from instructor The keyword all specifies that duplicates not be removed. select all dept_name from instructor The select Clause (Cont.) An asterisk in the select clause denotes “all attributes” select * from instructor The select clause can contain arithmetic expressions involving the operation, +, –, , and /, and operating on constants or attributes of tuples. The query: select ID, name, salary/12 from instructor would return a relation that is the same as the instructor relation, except that the value of the attribute salary is divided by 12. The where Clause The where clause specifies conditions that the result must satisfy Corresponds to the selection predicate of the relational algebra. To find all instructors in Comp. Sci. dept with salary > 70000 select name from instructor where dept_name = ‘Comp. Sci.' and salary > 70000 Comparison results can be combined using the logical connectives and, or, and not. Comparisons can be applied to results of arithmetic expressions. The from Clause The from clause lists the relations involved in the query Corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product instructor X teaches select  from instructor, teaches generates every possible instructor – teaches pair, with all attributes from both relations Cartesian product not very useful directly, but useful combined with where-clause condition (selection operation in relational algebra) instructor teaches Cartesian Product: instructor X teaches Joins For all instructors who have taught some course, find their names and the course ID of the courses they taught. select name, course_id from instructor, teaches where instructor.ID = teaches.ID Find the course ID, semester, year and title of each course offered by the Comp. Sci. department select section.course_id, semester, year, title from section, course where section.course_id = course.course_id and dept_name = ‘Comp. Sci.' Try Writing Some Queries in SQL Suggest queries to be written….. Natural Join Natural join matches tuples with the same values for all common attributes, and retains only one copy of each common column select * from instructor natural join teaches; Natural Join Example List the names of instructors along with the course ID of the courses that they taught. select name, course_id from instructor, teaches where instructor.ID = teaches.ID; select name, course_id from instructor natural join teaches; Natural Join (Cont.) Danger in natural join: beware of unrelated attributes with same name which get equated incorrectly List the names of instructors along with the the titles of courses that they teach Incorrect version (makes course.dept_name = instructor.dept_name) select name, title from instructor natural join teaches natural join course; Correct version select name, title from instructor natural join teaches, course where teaches.course_id = course.course_id; Another correct version select name, title from (instructor natural join teaches) join course using(course_id); The Rename Operation The Rename Operation The SQL allows renaming relations and attributes using the as clause: old-name as new-name E.g. select ID, name, salary/12 as monthly_salary from instructor Find the names of all instructors who have a higher salary than some instructor in ‘Comp. Sci’. select distinct T. name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’ Keyword as is optional and may be omitted instructor as T ≡ instructor T Keyword as must be omitted in Oracle

Use Quizgecko on...
Browser
Browser