Practice writing SQL statements Discuss SQL UNION statement.

Slides:



Advertisements
Similar presentations
Multiple Table Queries
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
MULTIPLE-TABLE QUERIES
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
ACCESS PART 2. Objectives Database Tables Table Parts Key Field Query and Reports Import from Excel Link to Excel.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
Chapter 2 Basic SQL SELECT Statements
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Concepts of Database Management Seventh Edition
Database Systems Microsoft Access Practical #3 Queries Nos 215.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
From Relational Algebra to SQL CS 157B Enrique Tang.
Agenda for 02/16/2006 Answer any questions about SQL project. Do you want to see any of the answers for the queries? Discuss additional formatting options.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Assignment 1 Uploaded to course website Due next Tuesday, Sep 1, at 11:59pm.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
(SQL - Structured Query Language)
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 Chapter 2 Basic SQL SELECT Statements. 2 Chapter Objectives Distinguish between an RDBMS and an ORDBMS Identify keywords, mandatory clauses, and optional.
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
5 Copyright © 2008, Oracle. All rights reserved. Testing and Validating a Repository.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
Writing Basic SQL SELECT Statements Lecture
Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must.
 CONACT UC:  Magnific training   
Database (Microsoft Access). Database A database is an organized collection of related data about a specific topic or purpose. Examples of databases include:
Objective The learner will solve & graph compound inequalities.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Retrieving Data Using the SQL SELECT Statement
Understanding Data Storage
MySQL Subquery Source: Dev.MySql.com
Chapter 6 - Database Implementation and Use
References: Text Chapters 8 and 9
Physical Changes That Don’t Change the Logical Design
Database Systems: Design, Implementation, and Management Tenth Edition
A Table with a View: Database Queries
Using the Set Operators
Relational Math CSC 240 (Blum).
MS Access Database Connection
Writing Basic SQL SELECT Statements
Using the Set Operators
Warm Up Use scalar multiplication to evaluate the following:
Chapter 4 Summary Query.
Bellwork 1/27 Solve the following by:
Databases Computer Technology.
Writing Basic SQL SELECT Statements
Databases Computer Technology.
Contents Preface I Introduction Lesson Objectives I-2
A Table with a View: Database Queries
Database Systems: Design, Implementation, and Management Tenth Edition
Queries and SQL in Access
Using the Set Operators
A Table with a View: Database Queries
Manipulating Data Lesson 3.
04 SQL & Relational Algebra
Presentation transcript:

Practice writing SQL statements Discuss SQL UNION statement. Agenda for Class (03/09/2006) Practice writing SQL statements Discuss SQL UNION statement. Provide guidelines for testing and debugging SQL.

A way to combine the result tables of separate SQL statements. What is a SQL UNION? A way to combine the result tables of separate SQL statements. Not really a “JOIN” statement. Does not combine underlying tables, as a JOIN statement. Combines result tables into a single result table. Produces a table of the concatenated results from SELECT statements.

Example of a UNION statement Imagine that we have two tables: A table containing our current customers (customer_tbl) and a table containing our past customers (old_customer_tbl). Imagine that we want to have a combined list of the names of both our current and old customers.

SELECT cust_name FROM customer_tbl UNION FROM old_customer_tbl; The UNION statement eliminates any items that are duplicated between the two result tables. It checks for duplication on the requested column or columns.

Example of a UNION ALL statement What if you want to see all of the results of the two select statements whether or not there are duplications? SELECT cust_name FROM customer_tbl UNION ALL FROM old_customer_tbl ORDER BY cust_name;

Example of an INTERSECT statement What if you want to look only at those rows that are common to both queries? SELECT cust_name FROM customer_tbl INTERSECT FROM old_customer_tbl ORDER BY cust_name;

Example of a MINUS statement What if you want to see only those rows that are in the current customer_tbl but that aren’t in the old_customer_tbl (past customers)? SELECT cust_name FROM customer_tbl MINUS FROM old_customer_tbl ORDER BY cust_name;

Another example of the MINUS Or maybe you want to see only those rows in the old_customer_tbl that don’t also exist in the current customer_tbl. SELECT cust_name FROM old_customer_tbl MINUS FROM customer_tbl ORDER BY cust_name;

Can combine any “like” queries What if you want to see a list of customers and products together? SELECT cust_name FROM customer_tbl UNION SELECT prod_desc FROM products_tbl ORDER BY 1;

Results on previous page are fairly ugly Results on previous page are fairly ugly. Can be improved by sorting, formatting, and giving a column name. COLUMN custprod heading “Customers and Products” SELECT initcap(cust_name) CustProd FROM customer_tbl UNION SELECT initcap(prod_desc) FROM products_tbl ORDER BY 1;

So, what are the rules? Must have the same number of columns in the select lists of the queries that are combined. The columns must be of the same type when compared column-by-column in the select lists of the queries that are combined. The name(s) of the columns are taken from the first query. The ORDER BY clause is best used with positioning rather than the name(s) of columns.

IS 475/675 - SQL Union Statement Debugging SQL Code SQL code design advice: Break the problem down into pieces. DO NOT try to write the entire solution in a single query. Think of dividing the problem into a set of objectives (join tables, eliminate rows, group by) and then write the code that achieves each objective. Use VIEWs to facilitate the division of code into manageable sections. Debugging DO NOT try to write the entire solution in a single query. Write the solution incrementally. Use SQL to help you determine the correct answer so that you know what you should get before writing the actual code. Determine how the code works in each step of the query execution process.