Chapter 3 Introduction to SQL Yonsei University 1 st Semester, 2015 Sanghyun Park.

Slides:



Advertisements
Similar presentations
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Advertisements

Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 3: SQL Bambang Irawan Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
E-R Diagram for a Banking Enterprise
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts - 6 th Edition SQL Schema Changes and table updates instructor teaches.
SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
CS 370 Database Systems Lecture 12 Introduction to SQL.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
INTRODUCTION TO SQL Database System Concepts. Agenda Overview of the SQL Query Language Data Definition Basic Query Structure Additional Basic Operations.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3: Introduction to SQL
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 5-1: Introduction to SQL. 2 Overview of the SQL Query Language Data Definition Basic Query Structure Additional Basic Operations Set Operations.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Database System Concepts, 6 th Ed. Chapter 6: Formal Relational Query Languages.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 3 Introduction to SQL
Chapter 3 Introduction to SQL(2)
Database Systems SQL cont. Relational algebra
Introduction to SQL.
Chapter 3 Introduction to SQL(3)
Chapter 3: Introduction to SQL
Chapter 6: Formal Relational Query Languages
Chapter 4: Intermediate SQL Joins
Chapter 3: Introduction to SQL
Chapter 3: SQL.
Chapter 3: Introduction to SQL
Chapter 3: Introduction to SQL
Chapter 6: Formal Relational Query Languages
Chapter 3: Introduction to SQL
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 6: Formal Relational Query Languages
Chapter 2: Intro to Relational Model
Chapter 3: Introduction to SQL
Lecture 3 Structure Query Language
Presentation transcript:

Chapter 3 Introduction to SQL Yonsei University 1 st Semester, 2015 Sanghyun Park

Outline  History  Basic Structure  Aggregate Functions  Nested Subqueries  Database Modification

History  IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory  Renamed Structured Query Language (SQL)  ANSI and ISO standard SQL  SQL-86, SQL-89, SQL-92  SQL:1999 (language name became Y2K compliant)  SQL:2003  Commercial systems offer most SQL-92 features, plus varying feature sets from later standards and special proprietary features

Schema Diagram for University Database

Basic Structure  SQL is based on set and relational operations with certain modifications and enhancements  A typical SQL query has the form: selectA 1, A 2,..., A n fromr 1, r 2,..., r m whereP  A i s represent attributes  r i s represent relations  P is a predicate  This query is equivalent to the relational algebra expression:  ∏ A1, A2,..., An (  P (r 1 x r 2 x... x r m ))  The result of an SQL query is a relation

SELECT Clause  The select clause lists the attributes desired in the query result  An asterisk in the select clause denotes “all attributes”  The select clause can contain arithmetic expressions  SQL allows duplicates in relations as well as in query results  Find department names of all instructors w/o duplicates: selectdistinct dept_name frominstructor  The keyword all specifies that duplicates not be removed: selectall dept_name frominstructor

WHERE Clause  The where clause specifies conditions the result must satisfy  To find all instructors in the Comp. Sci. dept with salary > 80000: selectname frominstructor wheredept_name = ‘Comp. Sci’ and salary >  Comparison results can be combined using the logical connectives and, or, and not  Comparisons can be applied to results of arithmetic expressions  SQL includes a between comparison operator

FROM Clause  The from clause lists the relations involved in the query  Find the Cartesian product instructor x teaches: select* frominstructor, teaches  Find the course ID, semester, year and title of each course offered by the Comp. Sci. dept: selectsection.course_id, semester, year, title fromsection, course wheresection.course_id = course.course_id and dept_name = ‘Comp. Sci.’

Aggregate Functions  These functions operate on the multiset of values of a column, and return a single value  avg, min, max, sum, count  Find the average salary of instructors in the Comp. Sci. dept: selectavg (salary) frominstructor wheredept_name = ‘Comp. Sci’  Find the names and average salaries of all departments whose average salary is greater than 42000: selectdept_name, avg (salary) frominstructor group bydept_name havingavg (salary) > 42000

Nested Subqueries  SQL provides a mechanism for the nesting of subqueries  A subquery is a select-from-where expression that is nested within another query  A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality  Find courses offered both in Fall 2009 and in Spring 2010: selectdistinct course_id fromsection wheresemester = ‘Fall’ and year = 2009 and course_id in (select course_id fromsection wheresemester = ‘Spring’ and year = 2010)

Database Modification - Delete  Delete all instructors from the Finance dept: delete frominstructor wheredept_name = ‘Finance’  Delete all instructors whose salary is less than the average salary of instructors: delete frominstructor wheresalary < (select avg (salary) from instructor)

Database Modification - Insert  Add a new tuple to course: insert into course values(‘CS-437’, ‘Database Systems’, ‘Comp. Sci’, 4) or equivalently insert into course(course_id, title, credits, dept_name) values(‘CS-437’, ‘Database Systems’, 4, ‘Comp. Sci’)  Add all instructors to the student relation with tot_creds set to 0: insert intostudent selectID, name, dept_name, 0 frominstructor

Database Modification - Update  Increase salaries of instructors whose salary is over $100,000 by 3% and all others receive 5% raise: updateinstructor setsalary = salary  1.03 wheresalary > ; updateinstructor setsalary = salary  1.05 wheresalary   The order is important  Can be done better using the case statement updateinstructor setsalary = case when salary  then salary *1.05 else salary * 1.03 end