SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.

Slides:



Advertisements
Similar presentations
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
Advertisements

E-R Diagram for a Banking Enterprise
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL Database Management Systems I Alex Coman, Winter 2006.
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
©Silberschatz, Korth and Sudarshan1.1Database System Concepts - 5 th Edition, May 23, 2005 Introduction to Database System Concepts.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Aandachtspunten Deadlines – alles op tijd inleveren! n Lees goed wat van je verwacht wordt!
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
SQL Lecture 3 SQL Lecture 3. 2 SQL SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex.
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
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.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 3: SQL.
-- Introduction to database principles Maoying Wu March 11, 2013 Chapter 3: Basic SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©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.
3.1 Chapter 3: SQL. 3.2 Chapter 3: SQL Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Relational Model: Examples. Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account.
SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language” (create.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Relational Model.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts, 5 th Edition, Oct 5, 2006 SQL Data Definition Basic Query Structure Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 5: Other Relational Languages.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
MySQL Tutorial Introduction to Database. Introduction of MySQL  MySQL is an SQL (Structured Query Language) based relational database management system.
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
Nested Subqueries in SQL By Kenneth Cheung CS 157A Section 2 Professor Lee.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL Bambang.
Chapter 5: SQL. Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
SQL and Relational Algebra
Structured Query Language
Chapter 3: SQL.
Module 4: SQL.
Structured Query Language (Data Manipulation Language)
Chapter 3: SQL.
Structured Query Language (SQL)
Chapter 3: SQL.
Chapter 3: SQL.
Instructor: Mohamed Eltabakh
Introduction to Database
Presentation transcript:

SQL Structured Query Language Meizhen Huang

Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions

Background SQL – Structured Query Language Developed by IBM in the 1970 ’ s, originally called Sequel The standard relational-database language Uses relational-algebra and relational- calculus constructs

Parts of SQL DDL: commands for defining relation schemas, deleting relations, and modifying relation schemas. DML: based on the relational algebra and the tuple relational calculus. Integrity: commands for specifying integrity constraints for the data in the DB. Authorization: commands for specifying access rights to relations and views.

Relations using in Examples Branch-schema = (branch-name, branch-city, assets) Customer-schema = (customer-name, customer- street, customer-city) Loan-schema = (loan-number,branch-name, amount) Borrower-schema = (customer-name, loan-number) Account-schema = (account-number, branch-name, balance) Depositor-schema = (customer-name, account- number)

Basic Structure Basic structure of SQL includes three clauses: select, from and where. A typical SQL has the form select A1, A2, …, An from r1,r2, …,rm where P Ai – an attribute ri – a relation p – a predicate This query is equivalent to the relational algebra expression:  A1, A2,..., An (  P (r 1 x r 2 x... x r m ))

select select – projection in RA select without elimination of duplicates “ Find the names of all branches in the loan relation. ” select branch-name select all branch-name from loan from loan  select with elimination of duplicates select distinct branch-name from loan

select  “ * ” can be used to denote “ all attributes ”. select * from loan The select clause may also contain arithmetic expressions involving the operators +, -, *, and / operating on constants or attributes of tuples. select loan-number, branch-name, amount*100 from loan

where where – selection predicate of RA e.g. “ Find all loan numbers for loans made at the Perryridge branch with loan amounts greater that $1200. ” select loan-number from loan where branch-name = ‘ Perryridge ’ and amount > 1200 Note: SQL uses and, or, and not instead of , v, and  in the where clause.  e.g., select loan-number from loan where amount between and Note: similarly, we can use the not between comparison operator.

from from – Cartesian-product in RA. “ Find the Cartesian product borrower x loan ” select  from borrower, loan

from n SQL uses relation-name.attribute-name, as does relational algebra, to avoid ambiguity n “ Find the name, loan number and loan amount of all customers having a loan at the Perryridge branch. ” select customer-name, borrower.loan-number, amount from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = ‘ Perryridge ’

Rename Rename can be operated on both relations and attributes. old-name as new-name  e.g., select customer-name, borrower.loan- number as loan-id, amount from borrower, loan where borrower.loan-number = loan.loan-number

Tuple Variables Tuple variables are defined in the from clause by way of the as clause. “ Find all customers who have a loan from the bank, find their names, loan numbers, and loan amount. ” select customer-name, T.loan-number, S.amount from borrower as T, loan as S where T.loan-number = S.loan-number

Tuple Variables Tuple variables are most useful for comparing two tuples in the same relation. “ Find the names of all branches that have assets greater than at least one branch located in Brooklyn. ” select distinct T.branch-name from branch as T, branch as S where T.assets > S.assets and S.branch-city = ‘ Brooklyn ’

String Operations The strings are enclosed by single quotes, for example, ‘ Perryridge ’. The most commonly used operation on strings is pattern matching using “ like ”. Pattern has two special characters: * Percent(%):matches any substring * Underscore(_): matches any character - ‘ Perry% ’ matches any string beginning with “ Perry ”. - ‘ _ _ _ % ’ matches any string of at least 3 characters. Note: Patterns are case sensitive.

String Operations “ Find the names of all customers whose street address includes the substring ‘ Main ’. ” select customer-name from customer where customer-street like ‘ %Main% ’

Ordering the Display of Tuples The order by clause list the result in sorted order. select distinct customer-name from borrower, loan where borrower.loan-number = loan.loan-number and branch- name = ‘ Perryridge ’ order by customer-name Note: by default, the order by clause lists items in ascending order. ( desc or asc ) select * from loan order by amount desc, loan-number asc

Set Operations The set operations union, intersect, and except operate on relations and correspond to the relational algebra operations  union all, intersect all and except all.

Union “ Find all customers having a loan, an account, or both at the bank. ” (select customer-name from depositor) union (select customer-name from borrower)

Intersect “ Find all customers who have both a loan and an account at the bank. ” (select distinct customer-name from depositor) intersect (select distinct customer-name from borrower)

Except “ Find all customers who have an account but no loan at the bank. ” (select distinct customer-name from depositor) except (select customer-name from borrower)

Aggregation Functions Aggregation functions take a collection of values as input and return a single value. * Average: avg (number) * Minimum: min * Maximum: max * Total: sum (number) * Count: count

avg “ Find the average account balance at the Perryridge branch. ” select avg (balance) from account where branch-name = ‘ Perryridge ’

group by Aggregation function can be applied to a group of sets of tuples by using group by clause. “ Find the average account balance at each branch. ” select branch-name, avg(balance) from account group by branch-name

having It is useful to state a condition that applies to groups rather than to tuples. “ Find the branches where the average account balance is more than $1200. ” select branch-name, ave(balance) from account group by branch-name having avg(balance) > 1200

The End