In this Lecture SQL SELECT. Example Tables. SQL SELECT Overview. WHERE Clauses. DISTINCT and ALL SQL SELECT. For more information



Similar documents
Analyse von großen Datensätzen in den Lebenswissenschaften (192217)

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

In This Lecture. Security and Integrity. Database Security. DBMS Security Support. Privileges in SQL. Permissions and Privilege.

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

In This Lecture. Physical Design. RAID Arrays. RAID Level 0. RAID Level 1. Physical DB Issues, Indexes, Query Optimisation. Physical DB Issues

Entity/Relationship Modelling. Database Systems Lecture 4 Natasha Alechina

SQL Data Definition. Database Systems Lecture 5 Natasha Alechina

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

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

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

Information Systems (Informationssysteme)

SQL SELECT Query: Intermediate

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

Advanced SQL - Subqueries and Complex Joins

Normalisation to 3NF. Database Systems Lecture 11 Natasha Alechina

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Modern Databases. Database Systems Lecture 18 Natasha Alechina

Relational Database: Additional Operations on Relations; SQL

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Lecture 4: More SQL and Relational Algebra

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

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

Querying Microsoft SQL Server 2012

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

Course ID#: W 35 Hrs. Course Content

Course 10774A: Querying Microsoft SQL Server 2012

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

Querying Microsoft SQL Server 20461C; 5 days

Where? Originating Table Employees Departments

Oracle Database 12c: Introduction to SQL Ed 1.1

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Querying Microsoft SQL Server

DATABASE DESIGN AND IMPLEMENTATION II SAULT COLLEGE OF APPLIED ARTS AND TECHNOLOGY SAULT STE. MARIE, ONTARIO. Sault College

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Introduction to Querying & Reporting with SQL Server

Information Systems SQL. Nikolaj Popov

The join operation allows you to combine related rows of data found in two tables into a single result set.

Displaying Data from Multiple Tables

Query-by-Example (QBE)

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

GET DATA FROM MULTIPLE TABLES QUESTIONS

Database Query 1: SQL Basics

Oracle SQL. Course Summary. Duration. Objectives

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

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

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

D B M G Data Base and Data Mining Group of Politecnico di Torino

SQL: Queries, Programming, Triggers

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

Supporting Data Set Joins in BIRT

Introduction to Microsoft Jet SQL

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Performing Queries Using PROC SQL (1)

Exercise 1: Relational Model

SQL: Queries, Programming, Triggers

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

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Querying Microsoft SQL Server 2012

Lecture 6. SQL, Logical DB Design

MOC QUERYING MICROSOFT SQL SERVER

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

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

CHAPTER 12. SQL Joins. Exam Objectives

SQL Programming. Student Workbook

WRITING EFFICIENT SQL. By Selene Bainum

Oracle Database: SQL and PL/SQL Fundamentals NEW

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

SQL Basics. Introduction to Standard Query Language

RECURSIVE COMMON TABLE EXPRESSIONS DATABASE IN ORACLE. Iggy Fernandez, Database Specialists INTRODUCTION

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

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Querying Microsoft SQL Server Querying Microsoft SQL Server D. Course 10774A: Course Det ails. Co urse Outline

When a variable is assigned as a Process Initialization variable its value is provided at the beginning of the process.

IT2305 Database Systems I (Compulsory)

OPTIMIZING QUERIES IN SQL SERVER 2008

1Z0-117 Oracle Database 11g Release 2: SQL Tuning. Oracle

1.204 Lecture 3. SQL: Basics, Joins SQL

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

6 CHAPTER. Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following:

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Querying Microsoft SQL Server 2012

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Oracle Database: SQL and PL/SQL Fundamentals

Unit 10: Microsoft Access Queries

Oracle 10g PL/SQL Training

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

SQL Server. 1. What is RDBMS?

Querying Microsoft SQL Server (20461) H8N61S

Database Administration with MySQL

Improving SQL Server Performance

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

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

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

Informationslogistik Unit 10: OLTP, OLAP, SAP, Data Warehouse, and Object-relational Databases

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

Chapter 5. SQL: Queries, Constraints, Triggers

There are five fields or columns, with names and types as shown above.

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

Relational Division and SQL

Transcription:

In this Lecture SQL SELECT Database Systems Lecture 7 Natasha Alechina SQL SELECT WHERE clauses SELECT from multiple tables JOINs For more information Connolly and Begg Chapter 5 Ullman and Widom Chapter 6.1-6.3 SQL SELECT Overview Example Tables SELECT [DISTINCT ALL] <column-list> FROM <table-names> [WHERE <condition>] [ORDER BY <column-list>] [GROUP BY <column-list>] [HAVING <condition>] ([]- optional, -or) ID First Last S103 John Smith S104 Mary S105 Jane S106 Mark S107 John Course Code Title DBS Database Systems PR1 Programming 1 PR2 Programming 2 IAI Intro to AI Grade Mark DISTINCT and ALL WHERE Clauses Sometimes you end up with duplicate entries Using DISTINCT removes duplicates Using ALL retains them - this is the default SELECT ALL Last FROM Last Smith SELECT DISTINCT Last FROM Last Smith Usually you don t want all the rows A WHERE clause restricts the rows that are returned It takes the form of a condition - only those rows that satisfy the condition are returned Example conditions: Mark < 40 First = John First <> John First = Last (First = John ) AND (Last = Smith ) (Mark < 40) OR (Mark > 70) 1

WHERE Examples WHERE Example Grade WHERE Mark >= 60 Mark SELECT DISTINCT ID FROM Grade WHERE Mark >= 60 ID S103 S104 S107 Given the table Grade Write an SQL query to find a list of the ID numbers and marks in Mark IAI of students who have passed (scored 40 or higher) IAI ID Mark S103 58 S104 65 Solution We only want the ID and Mark, not the Code Single quotes around the string SELECT ID, Mark FROM Grade WHERE (Code = IAI ) AND (Mark >= 40) We re only interested in IAI We re looking for entries with pass marks Often you need to combine information from two or more tables You can get the effect of a product by using Table1, Table2... If the tables have columns with the same name ambiguity results You resolve this by referencing columns with the table name TableName.Column SELECT First, Last, Mark FROM, Grade WHERE (.ID = Grade.ID) AND (Mark >= 40) ID First Last S103 John Smith S104 Mary S105 Jane Grade S106 Mark ID Code Mark S107 John Are matched with the first entry from the table... And then with the second and so on SELECT... FROM, Grade WHERE... ID First Last Mark S103 John Smith S103 John Smith S103 John Smith S103 John Smith S103 John Smith S103 John Smith S103 John Smith S103 John Smith S104 Mary S104 Mary S104 Mary S104 Mary S104 Mary All of the entries from the Grade table 2

SELECT... FROM, Grade WHERE (.ID = Grade.ID) AND... ID First Last Mark S103 John Smith S103 John Smith S104 Mary S104 Mary S106 Mark S107 John S107 John S107 John SELECT... FROM, Grade WHERE (.ID = Grade.ID) AND (Mark >= 40) ID First Last Mark S103 John Smith S103 John Smith S104 Mary S104 Mary S106 Mark S107 John S107 John.ID Grade.ID SELECT First, Last, Mark FROM, Grade WHERE (.ID = Grade.ID) AND (Mark >= 40) First Last Mark John Smith 72 John Smith 58 Mary 68 Mary 65 Mark 43 John 76 John 60 When selecting from multiple tables you almost always use a WHERE clause to find entries with common values, Grade, Course WHERE.ID = Grade.ID AND Course.Code = Grade.Code Grade Course ID First Last Mark Code Title S103 John Smith DBS Database Systems S103 John Smith IAI Intro to AI S104 Mary PR1 Programming 1 S104 Mary IAI Intro to AI S106 Mark PR2 Programming 2 S107 John PR1 Programming 1 S107 John PR2 Programming 2 S107 John IAI Intro to AI.ID = Grade.ID Course.Code = Grade.Code JOINs can be used to combine tables There are many types of JOIN CROSS JOIN NATURAL JOIN OUTER JOIN OUTER JOINs are linked with NULLs - more later JOINs A CROSS JOIN B returns all pairs of rows from A and B A NATURAL JOIN B returns pairs of rows with common values for identically named columns and without duplicating columns returns pairs of rows satisfying a condition 3

CROSS JOIN NATURAL JOIN ID Name CROSS JOIN ID Name ID 123 Name John NATURAL JOIN ID Name Code DBS PRG DBS PRG CROSS and NATURAL JOIN A CROSS JOIN B is the same as A, B A NATURAL JOIN B is the same as SELECT A.col1, A.coln, [and all other columns apart from B.col1, B.coln] FROM A, B WHERE A.col1 = B.col1 AND A.col2 = B.col2...AND A.coln = B.col.n (this assumes that col1 coln in A and B have common names) s specify a condition which the pairs of rows satisfy ON <condition> Can also use USING (col1, col2, ) Chooses rows where the given columns are equal ID Name USING (ID) ID Name Buyer Name Budget Smith 100,000 150,000 Green 80,000 Property Address Price 15 High St 85,000 12 Queen St 125,000 87 Oak Row 175,000 Buyer Property ON Price <= Budget Name Budget Address Price Smith 100,000 15 High St 85,000 150,000 15 High St 85,000 150,000 12 Queen St 125,000 4

JOINs vs WHERE Clauses ON <condition> is the same as A, B WHERE <condition> USING(col1, col2,...) is the same as A, B WHERE A.col1 = B.col1 AND A.col2 = B.col2 JOINs (so far) are not needed You can have the same effect by selecting from multiple tables with an appropriate WHERE clause So should you use JOINs or not? Yes, because They often lead to concise queries NATURAL JOINs are very common No, because Support for JOINs varies a fair bit among SQL dialects AND... Writing Queries This Lecture in Exams When writing queries There are often many ways to write the query You should worry about being correct, clear, and concise in that order Don t worry about being clever or efficient Most DBMSs have query optimisers These take a user s query and figure out how to efficiently execute it A simple query is easier to optimise We ll look at some ways to improve efficiency later Track cid Num Title Time aid 1 1 Violent 239 1 1 2 Every Girl 410 1 1 3 Breather 217 1 1 4 Part of Me 279 1 2 1 Star 362 1 2 2 Teaboy 417 2 CD cid Title Price 1 Mix 9.99 2 Compilation 12.99 Artist aid Name 1 Stellar 2 Cloudboy This Lecture in Exams Find a list of all the CD titles. (1 mark) Find a list of the titles of tracks that are more than 300 seconds long. (2 marks) Find a list of the names of those artists who have a track on the CD with the title Compilation. (4 marks) Next Lecture More SQL SELECT Aliases Self-joins Subqueries IN, EXISTS, ANY, ALL For more information Connolly and Begg Chapter 5 Ullman and Widom Chapter 6 5