SQL. SQL Data Manipulation: Queries

Similar documents
SQL Data Definition: Table Creation

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

SQL functions fit into two broad categories: Data definition language Data manipulation language

Oracle Database 11g: SQL and PL/SQL Fundamentals

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Querying Data with Transact SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

UFCEKG 20 2 : Data, Schemas and Applications

Data Manipulation Language (DML)

DB2 SQL Class Outline

Chapter 3: Introduction to SQL

Full file at

CSC Web Programming. Introduction to SQL

Chapter 3: Introduction to SQL

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

The SQL data-definition language (DDL) allows defining :

12. MS Access Tables, Relationships, and Queries


MTA Database Administrator Fundamentals Course

Querying Data with Transact-SQL

Oracle Database: Introduction to SQL Ed 2

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Chapter 9: Working with MySQL

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

CMPT 354: Database System I. Lecture 3. SQL Basics

1) Introduction to SQL

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

Oracle Syllabus Course code-r10605 SQL

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

II (The Sequel) We will use the following database as an example throughout this lab, found in students.db.

Course Outline and Objectives: Database Programming with SQL

Simple SQL. Peter Y. Wu. Dept of Computer and Information Systems Robert Morris University

Relational Database Management Systems for Epidemiologists: SQL Part I

Relational Database Language

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

T-SQL Training: T-SQL for SQL Server for Developers

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Lecture 6 - More SQL

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Introduction to Computer Science and Business

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Database Lab Lab 6 DML part 3

Lesson 2. Data Manipulation Language

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

Oracle Database 10g: Introduction to SQL

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Querying Data with Transact-SQL

QQ Group

G64DBS Database Systems. Lecture 7 SQL SELECT. The Data Dictionary. Data Dictionaries. Different Sections of SQL (DDL) Different Sections of SQL (DCL)

SQL Retrieving Data from Multiple Tables

COMP 244 DATABASE CONCEPTS & APPLICATIONS

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

Teradata SQL Features Overview Version

EE221 Databases Practicals Manual

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

RETRIEVING DATA USING THE SQL SELECT STATEMENT

Mobile MOUSe MTA DATABASE ADMINISTRATOR FUNDAMENTALS ONLINE COURSE OUTLINE

RESTRICTING AND SORTING DATA

Full file at Chapter 2: An Introduction to SQL

Principles of Data Management

CS 582 Database Management Systems II

Greenplum SQL Class Outline

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

SQL Data Query Language

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

chapter 2 G ETTING I NFORMATION FROM A TABLE

COURSE STUDENT LEARNING OUTCOMES: See attached or in course s learn.unm.edu

Structure Query Language (SQL)

Querying Data with Transact-SQL

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

You can write a command to retrieve specified columns and all rows from a table, as illustrated

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

Chapter 7 Equijoins. Review. Why do we store data for articles and writers in two separate tables? How can we link an article to its writer?

DATABASE TECHNOLOGY. Spring An introduction to database systems

Advanced SQL Tribal Data Workshop Joe Nowinski

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

SQL - Data Query language

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

The SQL database language Parts of the SQL language

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Silberschatz, Korth and Sudarshan See for conditions on re-use

Transcription:

SQL Data Manipulation: Queries ISYS 464 Spring 2003 Topic 09 1 SQL SQL: Structured Query Language Pronounced: "S Q L" or "sequel" Developed by IBM in San Jose for its experimental relational database management system called System R, which eventually became DB2 Originally called SEQUEL. Name changed to Structured Query Language or SQL. ANSI standards for SQL in 1989, 1992, 1999 Implemented in all relational DBMS: Oracle, Sybase, SQL Server, DB2, etc. This class covers Oracle 9i SQL*Plus. Most implementations have extensions beyond standard Standard and most implementations use terms table, row, column (not relation, tuple, attribute) 2 Select Statement One statement used for all SQL queries: SELECT statement Basic syntax of SELECT statement: SELECT column names FROM table names WHERE conditions Usually each clause starts on a new line Result of statement execution is a table that is displayed on the screen How do we do projection, selection, and join with the SELECT statement? Database (, Name, Major) (, Name, Day, Time) (, ) Major Name 123 Joe IS 234 Fred IS 345 Mary Acct 456 Sue Mgmt 567 Lee Acct Name Day Time ISYS 365 Adv C++ MW 12:30 ISYS 464 Database TTh 9:30 ISYS 565 Data Comm TTh 14:00 IBUS 330 Int Business MW 8:00 FIN 350 Finance MW 14:00 MGMT 405 Org Behavior MW 15:30 MKTG 431 Marketing TTh 8:00 123 ISYS 365 123 FIN 350 123 MGMT 405 234 ISYS 365 234 FIN 350 234 MGMT 405 234 MKTG 431 345 ISYS 464 567 ISYS 464 567 ISYS 565 567 MGMT 405 678 ISYS 363 678 MGMT 405 3 4 User-defined Names Projection Example User-defined names used for tables, columns, etc. No standard syntax Oracle syntax for user-defined names 1 to 30 characters Letters, digits, and the symbols $, #, and _ Spaces are not allowed in names: use an underscore symbol ( _ ) in place of a space Names are not case sensitive Cannot be a reserved word (e.g., SELECT) Query 1. What are the names of all students? SELECT _Name Name Joe Fred Mary Sue Lee 5 6. Not for general distribution. 1

Projection Example Query 2. What courses are taught on which days? SELECT _, Day FROM Day ISYS 365 MW ISYS 464 TTh ISYS 565 TTh IBUS 330 MW FIN 350 MW MGMT 405 MW MKTG 431 TTh Projection Example Query 3: What are all the majors taken by students? SELECT Major Is this a relation? SQL does not automatically delete duplicate rows To delete duplicate rows, include DISTINCT keyword: SELECT DISTINCT Major Major IS IS Acct Mgmt Acct Major IS Acct Mgmt 7 8 Selection Example Query 4. What is all the student course data for ISYS 464? _ WHERE _ = 'ISYS 464' Note: * means all columns 345 ISYS 464 567 ISYS 464 Selection Example Query 5. What is all the course data for ISYS 464? FROM WHERE _ = 'ISYS 464' Name Day Time ISYS 464 Database TTh 9:30 Why does 5 produce only 1 row but 4 produces 2 rows? 9 10 Selection Example Query 6: What is all the student data for student 382? WHERE _ = 382 What is the result of this query? Is this a valid query? Conditions: Comparison Comparison operators: =, <, >, <=, >=, <> WHERE _ >= 400 Character data Must be enclosed in single quotes in conditions Comparison is case sensitive: _ = 'ISYS 464' is not the same as _ = 'Isys 464' 11 12. Not for general distribution. 2

Conditions: Logical Logical operators: AND, OR, NOT Examples: WHERE _ >= 400 AND _ <= 600 WHERE Major = 'IS' OR Major = 'Acct' WHERE NOT Major = 'IS' NOT Major = 'IS', Major NOT = 'IS', Major <> 'IS' are equivalent Conditions: Logical Multiple AND, OR, NOT order of evaluation: 1. comparison 2. NOT 3. AND 4. OR WHERE _ >= 400 AND _ <= 600 OR NOT Major = 'IS' AND NOT Major = 'Acct' 13 14 Conditions: Logical Use parentheses to change order of evaluation Examples: WHERE (_ >= 400 AND _ <= 600) OR (NOT Major = 'IS' AND NOT Major = 'Acct') WHERE _ >= 400 AND (_ <= 600 OR NOT Major = 'IS') AND NOT Major = 'Acct' Conditions: Range Range operator: BETWEEN Examples: WHERE _ BETWEEN 400 AND 600 WHERE _ NOT BETWEEN 400 AND 600 Note: Range includes endpoints 15 16 Conditions: Set Membership Set membership operator: IN Examples: WHERE Major IN ('IS', 'Acct') WHERE Major NOT IN ('IS', 'Acct') Conditions: Pattern Match Pattern match operator: LIKE Wild card characters: % (percent) means any zero or more characters _ (underscore) means any one character Examples: WHERE Major LIKE 'I_' WHERE Major NOT LIKE 'A%' 17 18. Not for general distribution. 3

Conditions: NULL Operator: IS NULL Examples: WHERE Major IS NULL WHERE _Name IS NOT NULL Note: NULL is not the same as a blank space WHERE Major = ' ' Selection and Projection Example Query 7. What are the numbers of all students taking ISYS 464? SELECT WHERE _ = 'ISYS 464' Order of operations: Not specified in statement Determined by DBMS software (query optimization) Probable order: Step 1: Select Step 2: Project 345 ISYS 464 567 ISYS 464 345 567 19 20 Join Example Query 8. What is all the student course data and the student data for all the students enrolled in courses? _, WHERE _._ =._ Note use of qualification for column names because names are not unique in the database. Syntax: tablename.columnname Major Name 123 ISYS 365 123 Joe IS 123 FIN 350 123 Joe IS 123 MGMT 405 123 Joe IS 234 ISYS 365 234 Fred IS 234 FIN 350 234 Fred IS 234 MGMT 405 234 Fred IS 234 MKTG 431 234 Fred IS 345 ISYS 464 345 Mary Acct 567 ISYS 464 567 Lee Acct 567 ISYS 565 567 Lee Acct 567 MGMT 405 567 Lee Acct This is an inner join. It is an equijoin but not a natural join 21 Outer Joins Specified in WHERE clause FULL OUTER JOIN LEFT OUTER JOIN RIGHT OUTER JOIN _ FULL OUTER JOIN ON _._ =._ 22 Join with Composite Keys If the primary key of a table to be joined is composite, the corresponding foreign key will be composite The join condition must compare all columns in the composite keys using the AND operator FirstTab (A_col, B_col, C_col, D_col) SecondTab (E_col, F_Col, A_col, B_col) FROM FirstTab, SecondTab WHERE FirstTab.A_col = SecondTab.A_col AND FirstTab.B_col = SecondTab.B_col Selection, Projection, and Join Example Query 9. What are the names of all students taking ISYS 464? SELECT _Name _, WHERE _._ =._ AND _ = 'ISYS 464' Order of operations: Not specified in statement Determined by DBMS software (query optimization) 23 24. Not for general distribution. 4

Selection, Projection, and Join Example Possible order of operations I Step 1: Join Major Name 123 ISYS 365 123 Joe IS 123 FIN 350 123 Joe IS 123 MGMT 405 123 Joe IS 234 ISYS 365 234 Fred IS 234 FIN 350 234 Fred IS 234 MGMT 405 234 Fred IS 234 MKTG 431 234 Fred IS 345 ISYS 464 345 Mary Acct 567 ISYS 464 567 Lee Acct 567 ISYS 565 567 Lee Acct 567 MGMT 405 567 Lee Acct Step 2: Select Major Name 345 ISYS 464 345 Mary Acct 567 ISYS 464 567 Lee Acct Step 3: Project Name Mary Lee Selection, Projection, and Join Example Possible order of operations II Step 1: Select 345 ISYS 464 567 ISYS 464 Step 3: Project Name Mary Lee Step 2: Join Major Name 345 ISYS 464 345 Mary Acct 567 ISYS 464 567 Lee Acct Which order of operations is better I or II? Why? 25 26 Join Example Query 10: For each course that each student is taking, what is the student number, student name, and course number SELECT._, _Name,, WHERE _._ =._ Name 123 Joe ISYS 365 123 Joe FIN 350 123 Joe MGMT 405 234 Fred ISYS 365 234 Fred FIN 350 234 Fred MGMT 405 234 Fred MKTG 431 345 Mary ISYS 464 567 Lee ISYS 464 567 Lee ISYS 565 567 Lee MGMT 405 Subqueries A query (SELECT statement) within another SQL statement Query 9 (using a subquery). What are the names of all students taking ISYS 464? SELECT _Name WHERE._ IN (SELECT _. WHERE _ = 'ISYS 464') How is the result obtained? 27 28 Step 1: Perform subquery Subqueries 345 567 This creates a set of values (345, 567) for the IN operator in the main query Step 2: Perform main query Name Mary Lee Join vs. Subquery All subqueries can be done with a join Not all joins can be done with a subquery Query 9 can be done with a subquery but Query 10 cannot be done with a subquery. Why? What types of joins cannot be done with a subquery? Subqueries can have subqueries within them Subqueries are used in other SQL statements besides the SELECT statement 29 30. Not for general distribution. 5

Multiple Table Joins More than two tables can be joined at one time Three table joins are commonly used with manyto-many relationships Query 11: For each course that each student is taking, what is the student number, student name, course number, and course name? SELECT._, _Name,._, _Name, _, WHERE._ = _._ AND _._ =._ Notes on Performance Joins are slow relative to projection and selection Multiple-table joins are even slower Joins and especially multiple-table joins should be avoided unless absolutely necessary Tables that are joined should be as small as possible for better performance 31 32 Table Name Aliases Aliases for table names can be used to shorten a statement An alias comes after the table name in the FROM clause Query 10 (using table name aliases): For each course that each student is taking, what is the student number, student name, and course number? SELECT S._, S._Name, SC._ S, _ SC WHERE S._ = SC._ S is an alias for ; SC is an alias for _ If a table is defined with a table name alias in a statement the alias must be used to refer to the table; the table name cannot be used 33 Column Name Alias Aliases for column names can be defined in the SELECT clause Alias displays at the top of the column rather than the column name A column name alias cannot be used in the rest of the statement except under certain circumstances 34 Example: Column Name Alias Query 10 (using column name aliases and table name aliases): For each course that each student is taking, what is the student number, student name, and course number? SELECT S._ AS " Num", S._Name AS Name, SC._ AS S, _ S WHERE S._ = SC._ Num is an alias for S._; Name is an alias for S._Name; is an alias for SC._. These aliases will be displayed as the column headings for the output rather than the column names. AS is optional Note: Num must be in double quotes because it has a space. Do not use an SQL reserved word (e.g., ) as a column name alias unless it is enclosed in double quotes. 35 Expressions Expressions can be used in SELECT and WHERE clauses to do calculations Allowable operators: +, -, *, / SampleTab (A_col, B_col, C_col, D_col) SELECT B_col + C_col FROM SampleTab WHERE C_col > 5 * D_col Order of evaluation is standard (* and / left to right followed by + and - left to right); parentheses can be used to change order of evaluation Column name aliases often used in these types of queries to give a meaningful name to the column with the calculation 36. Not for general distribution. 6

Functions Built-in functions can be used in SELECT statements Many functions are available Two main types: Single-row functions: operate on a value in a single row Group (or multiple-row) functions: operate on values in groups of (or multiple) rows Single-Row Functions Operate on a value in a single row Character functions (22): UPPER(characters) converts characters to upper case functions (24): ROUND(number, n) rounds number to n places Date and time functions (24): MONTHS_BETWEEN(date1, date2) finds number of months between date1 and date2 Conversion functions (31): TO_CHAR(number) converts number to character data type Miscellaneous functions (37) 37 38 Example: Single-Row Function What are the numbers and names (in all upper case) of the students who are majoring in accounting? (Assume we do not know the case of Acct in the database.) SELECT _, UPPER(_Name) WHERE UPPER(Major) = 'ACCT' Group (Multiple-row) Functions Operate on values in groups of (or multiple) rows (26) AVG(parameter) finds average value SUM(parameter) finds total MIN(parameter) finds minimum value MAX(parameter) finds maximum value COUNT(parameter) counts number of rows or non- NULL values Parameter can be column name or * for all columns Group functions can only appear in the SELECT clause or the HAVING clause (described later); they cannot appear in the WHERE clause 39 40 Examples: Group Functions Assume that the _ table has a third column named Grade with the numeric grade (0 to 4) that the student received in the course Grade 123 ISYS 365 3 123 FIN 350 4 123 MGMT 405 1 234 ISYS 365 0 234 FIN 350 4 234 MGMT 405 NULL 234 MKTG 431 NULL 345 ISYS 464 3 567 ISYS 464 2 567 ISYS 565 3 567 MGMT 405 NULL 678 ISYS 363 3 678 MGMT 405 2 Examples: Group Functions What is the average grade? SELECT AVG(Grade) _ What are the minimum and maximum grades? SELECT MIN(Grade), MAX(Grade) _ What is the total of all the grades? SELECT SUM(Grade) _ 41 42. Not for general distribution. 7

COUNT Function COUNT(*) counts number of rows COUNT(column name) counts number of non- NULL values in the column COUNT (DISTINCT column name) counts the number of unique, non-null values in column 43 Examples: COUNT Function How many student/courses combinations are there? SELECT COUNT(*) _ How many student/course combinations have received grades? SELECT COUNT(Grade) _ How many student/course combinations have not received grades? SELECT Count(*) - Count(Grade) _ How many students are enrolled in courses? SELECT COUNT (DISTINCT _) _ 44 Grouping Output Output can be produced for groups of rows, such as subtotals for groups of output The GROUP BY clause is used to produce summary output in conjunction with a group function Examples: Grouping Output How many courses is each student taking? SELECT _, COUNT(*) _ GROUP BY _ COUNT(*) 123 3 234 4 345 1 456 3 567 2 What is the average grade of each student? SELECT _, AVG(Grade) _ GROUP BY _ If a column name is used in the SELECT clause it must be used in the GROUP BY clause If a column name is used in the GROUP BY clause it does not have to be used in the SELECT clause 45 46 Restricting Grouped Output HAVING option adds a condition to the GROUP BY clause HAVING is only used with GROUP BY What students are taking more than 2 courses? SELECT GROUP BY _ HAVING COUNT(*) > 2 Limiting Grouped Output If there is a WHERE clause and a GROUP BY with a HAVING clause: WHERE clause is evaluated first to determine what rows go into the groups Then the GROUP BY clause groups the resulting rows and the HAVING clause determines for which groups output is produced What majors among IS and Acct have more than two students? SELECT Major WHERE Major IN ('IS','Acct') GROUP BY Major HAVING COUNT(*) > 2 47 48. Not for general distribution. 8

Ordering Output Output may appear in any order, depending on how the data is stored and how the DBMS produces the output The ORDER BY clause produces output in specific order List students in alphabetical order by name SELECT _, _Name ORDER BY _Name 49 Ordering Output Ascending order (ASC) is assumed unless the word DESC is included List students in reverse order by number for all students whose number is less than 400 SELECT _, _Name WHERE _ < 400 ORDER BY _ DESC Rows can be ordered within groups List student course data in alphabetic order by course number and in numerical order by student number within course _ ORDER BY _, _ 50 Complete Syntax of SELECT Statement SELECT [DISTINCT] columnnamelist * FROM tablenamelist [WHERE condition] [GROUP BY columnnamelist [HAVING condition]] [ORDER BY columnname [ASC DESC] {,...}] 51. Not for general distribution. 9