Relational Database: Additional Operations on Relations; SQL



Similar documents
Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

SQL SELECT Query: Intermediate

Oracle SQL. Course Summary. Duration. Objectives

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Introduction to Microsoft Jet SQL

SQL. Short introduction

Oracle Database: SQL and PL/SQL Fundamentals

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Oracle Database: SQL and PL/SQL Fundamentals NEW

IT2304: Database Systems 1 (DBS 1)

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

IT2305 Database Systems I (Compulsory)

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Oracle Database: SQL and PL/SQL Fundamentals

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Chapter 5. SQL: Queries, Constraints, Triggers

SQL Server 2008 Core Skills. Gary Young 2011

Programming with SQL

Performing Queries Using PROC SQL (1)

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

How To Create A Table In Sql (Ahem)

Advance DBMS. Structured Query Language (SQL)

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Information Systems SQL. Nikolaj Popov

Databases 2011 The Relational Model and SQL

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

A basic create statement for a simple student table would look like the following.

Relational Algebra and SQL

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Query-by-Example (QBE)

SQL Basics. Introduction to Standard Query Language

PSU SQL: Introduction. SQL: Introduction. Relational Databases. Activity 1 Examining Tables and Diagrams

CS 338 Join, Aggregate and Group SQL Queries

Oracle Database 10g: Introduction to SQL

Oracle Database 12c: Introduction to SQL Ed 1.1

Database Query 1: SQL Basics

Instant SQL Programming

Introduction to SQL: Data Retrieving

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

SQL: Queries, Programming, Triggers

A Brief Introduction to MySQL

DBMS / Business Intelligence, SQL Server

1 Structured Query Language: Again. 2 Joining Tables

Database Administration with MySQL

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

ICAB4136B Use structured query language to create database structures and manipulate data

Lecture 4: More SQL and Relational Algebra

Financial Data Access with SQL, Excel & VBA

Structured Query Language (SQL)

Relational Division and SQL

Introduction to database design

MOC 20461C: Querying Microsoft SQL Server. Course Overview


Using AND in a Query: Step 1: Open Query Design

3.GETTING STARTED WITH ORACLE8i

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

CS 2316 Data Manipulation for Engineers

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

Lecture 25: Database Notes

Object-Oriented Query Languages: Object Query Language (OQL)

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Week 4 & 5: SQL. SQL as a Query Language

T-SQL STANDARD ELEMENTS

Chapter 1 Overview of the SQL Procedure

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Database CIS 340. lab#6. I.Arwa Najdi

SQL Server Database Coding Standards and Guidelines

Performance Tuning for the Teradata Database

Chapter 23. Database Security. Security Issues. Database Security

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Advanced Online Media Dr. Cindy Royal Texas State University - San Marcos School of Journalism and Mass Communication

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

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

Databases in Engineering / Lab-1 (MS-Access/SQL)

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Talking to Databases: SQL for Designers

Access Queries (Office 2003)

Inquiry Formulas. student guide

Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From

Welcome to the topic on queries in SAP Business One.

Netezza SQL Class Outline

Displaying Data from Multiple Tables

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

MySQL for Beginners Ed 3

Ad Hoc Advanced Table of Contents

Big Data and Analytics by Seema Acharya and Subhashini Chellappan Copyright 2015, WILEY INDIA PVT. LTD. Introduction to Pig


Introduction to SQL for Data Scientists

Querying Microsoft SQL Server

Oracle Database: SQL and PL/SQL Fundamentals NEW

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

Microsoft' Excel & Access Integration

Transcription:

Relational Database: Additional Operations on Relations; SQL Greg Plaxton Theory in Programming Practice, Fall 2005 Department of Computer Science University of Texas at Austin

Overview The course packet describes several simple aggregation operators The count operator Operators for numerical data: sum, average, minimum, maximum The course packet also describes the notion of grouping in the context of aggregation We will give a brief introduction to SQL in this lecture We will present examples of some key constructs As part of this introduction, we will see how aggregation and grouping are handled in SQL

Table Manipulation SQL includes a number of basic commands creating, removing, and maintaining tables CREATE TABLE: Create a table with the specified attributes DROP TABLE: Remove a table TRUNCATE TABLE: Remove the contents of a table, but not the table itself INSERT INTO: Add specified rows to a table UPDATE: Alter values within a table DELETE FROM: Delete rows satisfying a specified condition

SELECT The SELECT command is arguably the most important command in SQL It encompasses the selection, projection, and join operations that we discussed earlier in the context of relational algebra We will present a series of examples demonstrating some of the possible forms of a SELECT command Our examples will assume the existence of a parts relation with attributes partnum (integer), desc (string), price (float), and quantity (integer). We will follow the convention of capitalizing all the letters in SQL keywords

Projection via SELECT The following command performs a projection but retains duplicates: SELECT price, quantity FROM parts The following variation yields the projection operator we discussed earlier in the context of relational algebra: SELECT DISTINCT price, quantity FROM parts The asterisk symbol may be used as a shorthand to request all attributes: SELECT * FROM parts

WHERE Clause A WHERE clause can be used to perform selection within a SELECT command SELECT partnum, quantity FROM parts WHERE price > 10 SELECT partnum, 2 * quantity FROM parts WHERE price = 10 SELECT * FROM parts WHERE price > 10 AND price * quantity <= 1000 SELECT * FROM parts WHERE price BETWEEN 5 AND 10 SELECT * FROM parts WHERE desc IN ( nut, bolt ) SELECT * FROM parts WHERE desc LIKE %nut%

Specifying an Output Ordering An ORDER BY clause can be used to specify the output ordering SELECT * FROM parts ORDER BY partnum SELECT * FROM parts ORDER BY partnum DESC, price SELECT partnum, price * quantity FROM parts ORDER BY 2

Aggregation The operators MIN, MAX, AVG, and SUM can be used to aggregate numerical values SELECT MIN(price) FROM parts SELECT * FROM parts WHERE price > 0.25 * MAX(price) The operator COUNT can be used on any type of data SELECT COUNT(desc) FROM parts SELECT COUNT(DISTINCT desc) FROM parts WHERE price > 10

Grouped Aggregation Sometimes we would like to partition the tuples of a relation table into groups based on the values of certain attributes, and then aggregate over the groups SELECT desc, SUM(quantity) FROM parts GROUP BY desc

HAVING Clause A HAVING clause can be used to further filter the output of a grouped aggregation SELECT desc, SUM(quantity) FROM parts GROUP BY desc HAVING SUM(quantity)>20

Column Aliasing We can rename an attribute of the output relation by aliasing SELECT partnum "num", price * quantity "total" FROM parts

Operations Involving Multiple Relations Up to this point we have discussed SELECT commands involving a single relation (parts) Suppose now that we have an orders database with attributes customer (string), partnum (integer), quantity (integer)

Cross Product The cross product of two relations may be obtained using a SELECT command SELECT * FROM parts, orders SELECT customer, price, parts.quantity FROM parts, orders The above examples are somewhat unnatural More typically, we perform some form of a join on such a pair of relations Remark: Complex queries involving multiple tables are sometimes easier to read if each attribute name is prefixed with the associated table name (e.g., orders.customer rather than customer )

Inner Join (a.k.a. Equijoin) Quite commonly, a WHERE clause containing an equality constraint is used to join two relations SELECT customer, price FROM parts, orders WHERE parts.partnum = orders.partnum Here is an alternative way to express the same query SELECT customer, price FROM parts JOIN orders ON parts.partnum = orders.partnum

Table Aliasing It is often convenient to alias the name of a table within an SQL command involving multiple tables SELECT b.customer, a.price FROM parts a, orders b WHERE a.partnum = b.partnum

Natural Join Natural join is also defined within SQL, along with other variants Note that natural join can also be accomplished using an inner join In the case of our parts/orders relations, natural join is somewhat unnatural ; why?

Nested SELECT Commands The tables passed as arguments to a SELECT command can be produced by nested SELECT commands Arbitrary nesting is allowed Thus, a single SQL query can be quite complex