Chapter 7 SQL HUANG XUEHUA. SQL Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables;

Slides:



Advertisements
Similar presentations
Relational Algebra Relational algebra consists of a set of relational operators Each operator has one or more relations as input and creates a new relation.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
SQL Review.
Subqueries Example Find the name of the producer of ‘Star Wars’.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
Structured Query Language Part I Chapter Three CIS 218.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
Microsoft Access 2010 Chapter 7 Using SQL.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
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.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
More SQL: Complex Queries,
Structured Query Language
Chapter 3 Introduction to SQL(3)
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Lecture#7: Fun with SQL (Part 2)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
CS 405G: Introduction to Database Systems
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CS4222 Principles of Database System
SQL: Structured Query Language
Query Functions.
Chapter 2: Intro to Relational Model
Section 4 - Sorting/Functions
Example of a Relation attributes (or columns) tuples (or rows)
Lecture 2 Relational Database
Presentation transcript:

Chapter 7 SQL HUANG XUEHUA

SQL

Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables; SELECT title, length FROM movies WHERE year =1994; For example:

A typical SQL query has the form: select A 1, A 2,..., A n from r 1, r 2,..., r m where C  A i s represent attributes  r i s represent relations  C is a condition. This query is equivalent to the relational algebra expression. A1, A2,..., An (  c (r 1 x r 2 x... x r m )) The result of an SQL query is a relation. Select-From-Where Statements

SELECT GRAMMER SELECT [ALL | DISTINCT] [alias] [ , [alias] ]… FROM [ , ]… [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ASC | DESC]] ;

SELECT GRAMMER 1. Column can have this pattern : ( 1 ) * ( 2 ).* ( 3 ) COUNT([ALL | DISTINCT] * ) ( 4 ) [.] [alias] [ , [.] [alias]]… 2. WHERE condition expression is very flexible 3. GROUP BY: SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT- clause and each subgroup of tuples consists of the set of tuples that have the same value for the grouping attribute(s) HAVING only that group satisfy the condition can output 4. [ORDER BY [ASC | DESC]] : order

SELECT SEARCH 1. search based on single table ( 1 ) choose some columns : some columns ; all columns ; computed columns ( 2 ) choose some rows : eliminate some rows ; the tuples satisify the condition (比较大小,确定范围,确定集合,字符匹配,空值,多条件); ( 3 ) make the result in order 。 ( 4 ) use the aggregate function 。 ( 5 ) group 2. join search (等值与非等值连接;自身连接;外连接;复合条件连 接) 3. nest search (用 IN 子查询;用 = ;用 ANY 和 ALL ;用 EXISTS )

Search the Student Database Tables: student , course , sc ( 1 ) student ( sno , sname , ssex , sage,sdept ), ( 2 ) Course (cno , cname , cpno , ccredit) , ( 3 ) SC (sno , cno , grade)

STUDENT RELATION

SC RELATION

1) Search all the student’s information 2) Find the name and the number of the boys who study in ‘IS’ department? Select * from student Select sno , sname from student where ssex=‘m’ and sdept=‘IS’;

3)Search the students who have chosen courses. Select sno from sc

Search the Tuples Satisfy Condition Search condition operators comparision = , > , = , , ! , NOT+ 上述 符号 scope BETWEEN AND , NOT BETWEEN AND sets IN , NOT IN Letter match LIKE , NOT LIKE ( % , _ ) Null values IS NULL , IS NOT NULL Mult- conditions AND , OR

Search the Tuples Satisfy Condition 4)Search the name and sex of the student whose age is between 25 and 30 5)Search the student whose family name is’ 欧阳 ’ 6)Search the student who is from ‘IS’,’MATH’,’CS’ 7)Aggregate function

Use the Aggregate Function Aggregate function includes : COUNT ( [DISTINCT | ALL] * ) the numbers of the tuples COUNT ( [DISTINCT | ALL] ) the numbers of the columns SUM ( [DISTINCT | ALL] ) the total value of a column AVG ( [DISTINCT | ALL] ) the average value of a column MAX ( [DISTINCT | ALL] ) the maximum value of a column MAX ( [DISTINCT | ALL] ) the minimum value of a column 8). Search the total numbers of the students 。 select count(*) from student; 9). Search the numbers of the student who has chosen the courses select count(distinct sno) from sc;

10) Search the numbers of the students for each course. 11)Search the index number of the student who has chosen more than 4 courses SELECT Sno FROM SC GROUP BY Sno HAVING COUNT(*)>4 SELECT Cno, COUNT(Sno) FROM sc GROUP BY Cno;

Join 12) Search the student’s information and include the courses they have chosen and the grade they have got

Join Search

12a: 12b: Select student.*,sc.* From student,sc Where student.sno=sc.sno (等值连接)

Join Search 13) Search the course name of each course’s direct prerequisites 14)Search the student who have chosen the 2 course and the grade is large than 90

Summary From this lecture you can learn the basic syntax of data definition language. search condition Column expression join

Any Questions? If there are any outstanding questions you can ask me one-to-one after the lecture OR privately in my office.

Exercises Do the search exercises on the machine.