Chapter 3 Introduction to SQL(2)

Slides:



Advertisements
Similar presentations
E-R Diagram for a Banking Enterprise
Advertisements

Introduction to SQL Chapter 3 [1 of 2]
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
©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.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
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.
Chapter 3 Introduction to SQL Yonsei University 1 st Semester, 2015 Sanghyun Park.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
Chapter 3 Single-Table Queries
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
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.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Structured Query Language
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.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module A: Formal Relational.
Chapter 5-1: Introduction to SQL. 2 Overview of the SQL Query Language Data Definition Basic Query Structure Additional Basic Operations Set Operations.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Database System Concepts, 6 th Ed. Chapter 6: Formal Relational Query Languages.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
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 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.
Chapter 3 Introduction to SQL
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
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Database Systems Relational algebra SQL cont.
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
Chapter 2: Intro to Relational Model
Chapter 6: Formal Relational Query Languages
Chapter 27: Formal-Relational Query Languages
Chapter 3: Introduction to SQL
Lecture 3 Structure Query Language
Lab 2: Retrieving Data from the Database
Presentation transcript:

Chapter 3 Introduction to SQL(2) 2nd 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 A typical SQL query has the form: select A1, A2, ..., An from r1, r2, ..., rm where P Ais represent attributes ris represent relations P is a predicate The result of an SQL query is a relation

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

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 duplicates select distinct dept_name from instructor The keyword all specifies that duplicates should not be removed select all dept_name from instructor

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

Where Clause Where clause specifies conditions that the result must satisfy To find all instructors in Comp. Sci. dept select name from instructor where dept_name = ‘Comp. Sci.’ Comparison results can be combined using the logical connectives and, or, and not select name from instructor where dept_name = ‘Comp. Sci.’ and salary > 80000

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

Cartesian Product

Examples Find the names of all instructors who have taught some course and the course_id select name, course_id from instructor, teaches where instructor.ID = teaches.ID Find the names of all instructors in the Art department who have taught some course and the course_id select name, course_id from instructor, teaches where instructor.ID = teaches.ID and instructor.dept_name = ‘Art’

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.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

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

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 name from instructor where name like ‘%dar%’

Ordering the Display of Tuples List in alphabetic order the names of all instructors select distinct name from instructor order 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

Where Clause Predicates SQL includes a between comparison operator select name from instructor where salary between 90000 and 100000 Tuple comparison select name, course_id from instructor, teaches where (instructor.ID, dept_name) = (teaches.ID, ‘Biology’)