SQL Overview. CSCE 315, Fall 2017 Project 1, Part 3. Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

Similar documents
SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Chapter 3: Introduction to SQL

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

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

Slides by: Ms. Shree Jaswal

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

SQL DATA DEFINITION LANGUAGE

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Querying Data with Transact SQL

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Chapter 3: Introduction to SQL

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

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

SQL DATA DEFINITION LANGUAGE

Chapter 4: SQL. Basic Structure

Relational Algebra and SQL

1) Introduction to SQL

SQL DATA DEFINITION LANGUAGE

Principles of Data Management

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

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

SQL (Structured Query Language)

Programming and Database Fundamentals for Data Scientists

CSC Web Programming. Introduction to SQL

DATABASE TECHNOLOGY - 1MB025

COMP 430 Intro. to Database Systems

SQL: Data Definition Language

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

MTA Database Administrator Fundamentals Course

Chapter 6 The database Language SQL as a tutorial

DATABASE TECHNOLOGY - 1MB025

Full file at

Information Systems Engineering. SQL Structured Query Language DML Data Manipulation (sub)language

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

Data Manipulation Language (DML)

Databases-1 Lecture-01. Introduction, Relational Algebra

QQ Group

Querying Data with Transact-SQL

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

DATABASTEKNIK - 1DL116

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

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

The Relational Model of Data (ii)

Database design process

STRUCTURED QUERY LANGUAGE (SQL)

SQL Data Query Language

CGS 3066: Spring 2017 SQL Reference

Set Operations, Union

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

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

An Introduction to Structured Query Language

Chapter 3: SQL. Chapter 3: SQL

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

Lecture 04: SQL. Wednesday, October 4, 2006

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

Lecture 04: SQL. Monday, April 2, 2007

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

user specifies what is wanted, not how to find it

Database Systems SQL SL03

An Introduction to Structured Query Language

An Introduction to Structured Query Language

An Introduction to Structured Query Language

An Introduction to Structured Query Language

The SQL database language Parts of the SQL language

SQL. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

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

Relational Database Management Systems for Epidemiologists: SQL Part I

Midterm Review. Winter Lecture 13

Database Systems SQL SL03

SQL: Data Manipulation Language

Querying Data with Transact-SQL

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

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

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Oracle Database 11g: SQL and PL/SQL Fundamentals

CSEN 501 CSEN501 - Databases I

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

DATABASE DESIGN I - 1DL300

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

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

CS143: Relational Model

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Querying Data with Transact-SQL

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

After completing this unit, you should be able to: Define the terms

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

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

Introduction to Computer Science and Business

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

UFCEKG 20 2 : Data, Schemas and Applications

Transcription:

SQL Overview CSCE 315, Fall 2017 Project 1, Part 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

SQL Structured Query Language Database language used to manage and query relational databases A well-known, commonly used standard Regularly updated Many extensions, variations Platform-specific versions, etc.

Generations of Programming Languages 1 st generation Machine code 2 nd generation Human-readable but directly related to processor Assembly language, C (sort of) 3 rd generation Abstraction from processor, easier for humans Fortran, C/C++, Java, etc. 4 th generation Programming Language for specific task e.g. SQL, Matlab 5 th generation Give constraints (goal), and result follows logically e.g. Prolog

SQL Elements Data Definition Language (DDL) Supports creation of database schema Data Manipulation Language (DML) Supports entering/removing data Querying Language Supports query operations (don t change data itself) Others: Transaction control, Data control

Our Discussion of SQL Will highlight some of the structures and features of SQL Give you an idea of the basics of how it works Reflects how relational databases work Not meant to make you SQL programmers You will need to know parts of this for the first project

Database Schema The set of relations (tables) in the database. Create, delete, change tables

CREATE Define a relation CREATE TABLE <name> ( <element list> ); element = <name> <type>

Element Types INT, INTEGER Integers FLOAT, REAL Floating-Point numbers CHAR(n) Fixed-length string of n characters VARCHAR(n) Variable-length string of up to n characters DATE yyyy-mm-dd TIME hh:mm:ss

Example CREATE TABLE HouseRep ( Name VARCHAR(80), Party CHAR(10), Birthdate DATE, YearsInCongress INT, Salary REAL );

Declaring Keys Keys declared within CREATE statement Key attributes functionally determine all other attributes in the relation List under PRIMARY KEY Elements of primary key can not be NULL

Example CREATE TABLE HouseRep ( Name VARCHAR(80), Party CHAR(10), Birthdate DATE, YearsInCongress INT, Salary REAL, PRIMARY KEY (Name) );

Example CREATE TABLE HouseRep ( Name VARCHAR(80), Party CHAR(10), Birthdate DATE, YearsInCongress INT, Salary REAL, PRIMARY KEY (Name, Birthdate) );

Other Element Modifiers UNIQUE Placed after type Only one tuple in that relation for each value (except NULL) Can imply key if no primary key given Can be NULL NOT NULL Cannot take value NULL DEFAULT Default value specified

Example CREATE TABLE HouseRep ( Name VARCHAR(80) UNIQUE, Party CHAR(10), Birthdate DATE NOT NULL, YearsInCongress INT DEFAULT 0, Salary REAL DEFAULT 120000.00 );

Other Table Modifications DROP <name> Deletes that table ALTER TABLE <name> ADD <attribute> Adds a new column to table ALTER TABLE <name> DROP <attribute> Removes the column from the table

Views Views are a sort of virtual table, usually created as the result of a query Format: CREATE VIEW <name> AS <query>

Modifying the Database Data Manipulation Language Given a schema, must populate the database with actual data Insert, Delete, Modify

Insertion INSERT command: INSERT INTO <Relation> VALUES (<value list>); Can specify only certain attributes in Relation Relation(<attribute list>) Instead of values, can have subquery

Insertion Example Senator (Name, Party, State,Years) INSERT INTO Senator VALUES (Jill Smith, Republican, NY, 5); INSERT INTO Senator(Name, State) VALUES (Jill Smith, NY);

Deletion Delete from relation according to condition DELETE FROM <Relation> WHERE <condition>; Example: delete Texas Senators: DELETE FROM Senator WHERE State = TX ;

Modification Update subset according to condition UPDATE <Relation> SET <list of attribute assignments> WHERE <condition>; Example: Joe Lieberman becomes Independent UPDATE Senator SET Party = Independent WHERE Name = Joseph Lieberman ;

Queries The heart of SQL Queries can form portion of other commands e.g. INSERT results of a query into a table Form: SELECT attributes FROM relation(s) WHERE condition

Example Senator: Name Party State Years Jill Smith Republican NY 5 Query: SELECT Name FROM Senator WHERE Party = Republican ; Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 Result: Name Jill Smith Jim Brown

Statement Processing Begin with the relation(s) in the FROM clause Can be the result of another query! Apply selection condition in WHERE clause Can potentially be very complex, and include subqueries Get the attributes given in (more generally, apply a projection to) the SELECT clause Process: iterate through all tuples in FROM, checking vs. WHERE, and for those that match, apply the SELECT-

SELECT Clause - * Can use a * for SELECT to indicate all attributes given in the relation listed in FROM. Senator: Query: SELECT * FROM Senator WHERE Party = Republican ; Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 Result: Name Party State Years Jill Smith Republican NY 5 Jim Brown Republican PA 15

SELECT Clause - AS Can use AS to rename attributes in result Senator: Query: Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 SELECT Name AS Person, Party AS Affiliation, State FROM Senator WHERE Party = Republican ; Result: Person Affiliation State Jill Smith Republican NY Jim Brown Republican PA

SELECT Clause - Expression Can include expressions in SELECT Clause Senator: Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 Query: SELECT Name, Years * 365 AS DaysInOffice FROM Senator WHERE Party = Republican ; Result: Name DaysInOffice Jill Smith 1825 Jim Brown 5475

Aggregations SUM, AVG, COUNT, MIN, MAX COUNT(*) counts number of tuples The main purpose is to Aggregate a number of records to a single row Applied to column(s) in SELECT clause Example 1: Count the number of reps in US Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 SELECT COUNT(*)FROM USReps Count(*) 4

Aggregations SUM, AVG, COUNT, MIN, MAX COUNT(*) counts number of tuples The main purpose is to Aggregate a number of records to a single row Applied to column(s) in SELECT clause Example 2: Select Party and also Count the number of reps Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 SELECT Party, COUNT(*)FROM USReps

Grouping Aggregations Adding GROUP BY <attribute> at the end will apply aggregation only to group Example 3: Count the number of U.S. Representatives by Party (also mention which party) Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 SELECT Party, COUNT(*)FROM USReps GROUP BY Party Party Republican 2 Democrat 2 Count

HAVING Can restrict GROUP using HAVING HAVING can refer to the FROM clause and its attributes Example 4: Count representatives by party, but discard parties that have any representative with 3 years or less experience Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 SELECT Party, COUNT(*) FROM USReps GROUP BY Party HAVING MIN(Years) > 3 Party Republican 2 Count

WHERE Clause Complex Expressions Can include NOT, AND, OR operators Senator: Query: SELECT * FROM Senator Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 WHERE Party = Republican OR Years > 3; Result: Name Party State Years Jill Smith Republican NY 5 Sue Jones Democrat CT 9 Jim Brown Republican PA 15

WHERE Clause other effects Order of operations, including parentheses LIKE: String comparisons with wildcards % means any string _ means any character Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 SELECT * FROM Senator WHERE Name LIKE J% Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Jim Brown Republican PA 15

WHERE Clause NULL values Tuples may contain NULL values Undefined/Unknown Inapplicable All conditions evaluate to either TRUE, FALSE, or UNKNOWN Comparisons to NULL are UNKNOWN Tuples selected only if TRUE

3-valued Logic Can think of values as TRUE = 1 FALSE = 0 UNKNOWN = ½ Operations would be OR = MAX AND = MIN NOT = 1-x Example: (T AND ((NOT U OR F) AND NOT (U OR T)))

3-valued Logic Can think of values as TRUE = 1 FALSE = 0 UNKNOWN = ½ Operations would be OR = MAX AND = MIN NOT = 1-x Example: (T AND ((NOT U OR F) AND NOT (U OR T))) MAX(1- ½,0) = MAX(½,0) = ½ = U

3-valued Logic Can think of values as TRUE = 1 FALSE = 0 UNKNOWN = ½ Operations would be OR = MAX AND = MIN NOT = 1-x Example: (T AND (U AND NOT (U OR T)))

3-valued Logic Can think of values as TRUE = 1 FALSE = 0 UNKNOWN = ½ Operations would be OR = MAX AND = MIN NOT = 1-x Example: (T AND (U AND NOT (U OR T))) MAX(½, 1) = 1 = T

3-valued Logic Can think of values as TRUE = 1 FALSE = 0 UNKNOWN = ½ Operations would be OR = MAX AND = MIN NOT = 1-x Example: (T AND (U AND NOT T)

3-valued Logic Can think of values as TRUE = 1 FALSE = 0 UNKNOWN = ½ Operations would be OR = MAX AND = MIN NOT = 1-x Example: (T AND (U AND NOT T) ) MIN(½, 1-1) = MIN(½,0) = 0 = F

3-valued Logic Can think of values as TRUE = 1 FALSE = 0 UNKNOWN = ½ Operations would be OR = MAX AND = MIN NOT = 1-x Example: (T AND F)

3-valued Logic Can think of values as TRUE = 1 FALSE = 0 UNKNOWN = ½ Operations would be OR = MAX AND = MIN NOT = 1-x Example: (T AND F) MIN(0,1) = 0 = F

3-valued Logic Can think of values as TRUE = 1 FALSE = 0 UNKNOWN = ½ Operations would be OR = MAX AND = MIN NOT = 1-x Example: F (T AND ((NOT U OR F) AND NOT (U OR T)))

Unexpected Results for NULLs WHERE (Years > 2) OR (Years < 3) This should cover all cases If Years is NULL Years > 2 is UNKNOWN Years < 3 is UNKNOWN So the OR is UNKNOWN And thus the tuple is NOT selected!

WHERE Clause IN operator <tuple> IN <relation> TRUE iff the tuple is a member of the relation SELECT * FROM ElectedOfficial WHERE Name IN USRep Name Lloyd Doggett Bill Flores Result Party Democrat Republican ElectedOfficial Name Party Lloyd Doggett Democrat John Cornyn Republican John Adams Federalist Bill Flores Republican USRep Name Lloyd Doggett Bill Flores

WHERE Clause EXISTS operator EXISTS (<relation>) TRUE iff the relation is not empty relation SELECT * FROM ElectedOfficial WHERE EXISTS(USRep) Name Lloyd Doggett John Cornyn John Adams Bill Flores Result Party Democrat Republican Federalist Republican ElectedOfficial Name Party Lloyd Doggett Democrat John Cornyn Republican John Adams Federalist Bill Flores Republican USRep Name Bill Flores Lloyd Doggett

EXISTS (and other) operators Usually applied to the results of a subquery Example: is any Senator a Whig? EXISTS( ) SELECT * FROM Senator WHERE Party = Whig

WHERE Clause ANY and ALL operators x = ANY(<relation>) TRUE iff x is equal to at least one tuple in the relation x = ALL(<relation>) TRUE iff x is equal to all tuples in the relation The = can also be >, >=, <, <=, <> The relation should have only one attribute

Name Example: ANY Lloyd Doggett Bill Flores John Adams John Cornyn ElectedOfficial Party Democrat Republican Federalist Republican CurrentParties Name Democrat Republican SELECT * FROM ElectedOfficial WHERE Party = ANY (CurrentParties) Name Lloyd Doggett Bill Flores John Cornyn Result Party Democrat Republican Republican

Example: ALL Senator Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 YearsPresidentsInSenate Years Served SELECT * 0 FROM ElectedOfficial WHERE Years > ALL (YearsPresidentsInSenate) 6 0 12 6 Name Party State Years Jim Brown Republican PA 15

Example: ALL Senator Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 YearsPresidentsInSenate Years Served 6 0 12 SELECT * 0 FROM ElectedOfficial WHERE Party = ANY (CurrentParties) Name Party State Years Jim Brown Republican PA 15 6

Example: ALL Senator Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 YearsPresidentsInSenate Years Served 6 0 12 SELECT * 0 FROM ElectedOfficial WHERE Party = ANY (CurrentParties) Name Party State Years Jim Brown Republican PA 15 6

Example: ALL Senator Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 YearsPresidentsInSenate Years Served 6 0 12 SELECT * FROM ElectedOfficial 0 WHERE Party = ANY (CurrentParties) 6 Name Party State Years Jim Brown Republican PA 15

Example: ALL Senator Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 SELECT * FROM ElectedOfficial 0 WHERE Party = ANY (CurrentParties) YearsPresidentsInSenate Years Served 6 0 12 6 Name Party State Years Jim Brown Republican PA 15

Example: ALL Senator Name Party State Years Jill Smith Republican NY 5 Joe Adams Democrat NJ 0 Sue Jones Democrat CT 9 Jim Brown Republican PA 15 SELECT * 0 FROM ElectedOfficial WHERE Party = ANY (CurrentParties) YearsPresidentsInSenate Years Served 6 0 12 6 Name Party State Years Jim Brown Republican PA 15

UNION, INTERSECT, DIFFERENCE Can combine subqueries with Boolean operations e.g. (subquery) UNION (subquery) Default: duplicates are removed by these operations unless ALL is included (subquery) INTERSECT ALL (subquery) Likewise, can remove duplicates in normal SELECT by including DISTINCT SELECT DISTINCT Years

Bag vs. Set semantics Items are in a bag Duplicates OK Items are in a set Duplicates removed

Joins Combining relations into one new relation Many ways, variations <relation> CROSS JOIN <relation> Takes every possible combination

CROSS JOIN example VanTypes SeatsAndPaint Make Model Seats Paint Dodge Caravan Cloth Standard Honda Odyssey Leather Standard Leather Premium Result Make Model Seats Paint Dodge Caravan Cloth Standard Dodge Caravan Leather Standard Dodge Caravan Leather Premium Honda Odyssey Cloth Standard Honda Odyssey Leather Standard Honda Odyssey Leather Premium

Inner Joins Inner Joins are based on the Cross Join Join is usually limited by some comparison using ON (Theta Join) SELECT * FROM Senator INNER JOIN Representative ON Senator.State = Representative.State Creates table with one (Senator, Representative) tuple for every pair from the same state. (Note: both State attributes still appear)

Natural Joins Automatically looks for matching columns The ON keyword does not apply here Only one column for each match, and only select tuples that match in those columns SELECT * FROM Senator NATURAL JOIN Representative

Natural Join Example Name Joe Smith Jill Smith Sam Jones Sue Jones Students School Rice LSU Texas A&M Rice SchoolLocations School City Texas A&M College Station Rice Houston LSU Baton Rouge SELECT * FROM Students NATURAL JOIN SchoolLocations Result Name School City Joe Smith Rice Houston Jill Smith LSU Baton Rouge Sam Jones Texas A&M College Station Sue Jones Rice Houston

OUTER JOIN Includes tuples from both relations, even if no match in the other Those attributes are set to NULL LEFT, RIGHT, FULL Keep all records from left table, or from right table, or from both

Project 1 Checkpoint 3 Helpful SQL Query Tools (go this site): http://beekeeperdata.com/posts/2016/02/01/5-freequery-tools-for-sql.html Installs mini SQL engine and gives a platform to test queries Useful for verifying correctness of your implementation Implementing the Database::Query function Some Important Queries Implementation Issues

Some Example Queries SELECT * FROM ElectedOfficial WHERE Years > 5 SELECT * FROM ElectedOfficial WHERE Years > 5 AND Years < 7 SELECT * FROM ElectedOfficial WHERE Years > 5 AND Years < 7 OR (Years > Salary / 100 * 7 AND Years < Networth/1000) SELECT * FROM ElectedOfficial WHERE Years > 5 AND Years < 7 OR NOT (Years > Salary / 100 * 7-35) ORDER BY Salary

More Queries SELECT * FROM Invoice WHERE Invoice.total > (SELECT MAX (UnitPrice) FROM Track) SELECT * FROM Invoice WHERE Invoice.total > (SELECT MAX (UnitPrice) FROM Track) AND Invoice.total < 10

Other Queries ANY, ALL, EXISTS, IN Required for 4-person teams 3-person teams can get extra SELECT * FROM Invoice WHERE Invoice.total > ANY (SELECT UnitPrice FROM Track) Context, not independent!!! SELECT * FROM Invoice WHERE EXISTS (SELECT * FROM Track WHERE Invoice.total > UnitPrice)

Implementation Thought General Format: SELECT <columns> FROM <tablename> WHERE <condition> The condition can be: A logical expression within the table A number of logical expression separated by AND/OR/NOT A number of queries with AND/OR/NOT between them and also with IN/EXISTS/ANY

Query Implementation Thinking Recursive Process the rest of the query as a math expression Remember post-fix calculators!!! But when comes to another query inside one, call the same query function recursive As argument pass the context The recurse-d function is simpler It has to process a simpler query At some point, you will find a very simple query, and then start returning the result

Query Function: Pseudocode Table Query (vector<string> columns, string from-table, string condition) { 1. Parse condition string and identify parts, 2. Find their connecting conditions (AND/OR/NOT) 3. Evaluate each part a. If the part is an expression, evaluate that b. If the part looks like a query, (i.e., starting with a SELECT within a parenthesis), call the query function again to evalue it, pass along the context: from-table 4. Combine all parts and apply the AND/OR/NOT condition between them 5. Each row of the from-table is tested this way and either kept or thrown away 6. Return the kept rows and only the selected colums as table }