SQL: joins. Practices. Recap: the SQL Select Command. Recap: Tables for Plug-in Cars



Similar documents
SQL. Short introduction

How To Create A Table In Sql (Ahem)

A table is a collection of related data entries and it consists of columns and rows.

Introduction to Microsoft Jet SQL

MS ACCESS DATABASE DATA TYPES

Information Systems SQL. Nikolaj Popov

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

Financial Data Access with SQL, Excel & VBA

A Brief Introduction to MySQL

Using SQL Server Management Studio

Introduction to SQL for Data Scientists

Database Migration from MySQL to RDM Server

Intro to Databases. ACM Webmonkeys 2011

DDL is short name of Data Definition Language, which deals with database schemas and descriptions, of how the data should reside in the database.

Title. Syntax. stata.com. odbc Load, write, or view data from ODBC sources. List ODBC sources to which Stata can connect odbc list

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

Database Administration with MySQL

Linas Virbalas Continuent, Inc.

MySQL for Beginners Ed 3

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

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

SQL Server An Overview

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

3.GETTING STARTED WITH ORACLE8i

Database Query 1: SQL Basics

SQL Server Table Design - Best Practices

2/3/04 Doc 7 SQL Part 1 slide # 1

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

sqlite driver manual

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

not at all a manual simply a quick how-to-do guide

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

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

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

Oracle Database 10g Express

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

CSC 443 Data Base Management Systems. Basic SQL

B.1 Database Design and Definition

MYSQL DATABASE ACCESS WITH PHP

4 Simple Database Features

Microsoft Access 3: Understanding and Creating Queries

SQL Programming. Student Workbook

1 Structured Query Language: Again. 2 Joining Tables

Relational Database: Additional Operations on Relations; SQL

Oracle SQL. Course Summary. Duration. Objectives

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

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Once the schema has been designed, it can be implemented in the RDBMS.

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

4 Logical Design : RDM Schema Definition with SQL / DDL

IT2305 Database Systems I (Compulsory)

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

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

!"# $ %& '( ! %& $ ' &)* + ! * $, $ (, ( '! -,) (# *&23. mysql> select * from from clienti;

Oracle Database: SQL and PL/SQL Fundamentals NEW

IT2304: Database Systems 1 (DBS 1)

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

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

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

Relational databases and SQL

Instant SQL Programming

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

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

Extracting META information from Interbase/Firebird SQL (INFORMATION_SCHEMA)

Geodatabase Programming with SQL

Advance DBMS. Structured Query Language (SQL)

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

Consulting. Personal Attention, Expert Assistance

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

CHAPTER 1 Overview of SAS/ACCESS Interface to Relational Databases

Oracle Database: SQL and PL/SQL Fundamentals

ACCESS Importing and Exporting Data Files. Information Technology. MS Access 2007 Users Guide. IT Training & Development (818)

Click to create a query in Design View. and click the Query Design button in the Queries group to create a new table in Design View.

Oracle Database 11g SQL

Access Queries (Office 2003)

XEP-0043: Jabber Database Access

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Relational Databases. Christopher Simpkins

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Database Applications Microsoft Access

Talking to Databases: SQL for Designers

Oracle Database 12c: Introduction to SQL Ed 1.1

Structured Query Language (SQL)

Microsoft SQL connection to Sysmac NJ Quick Start Guide

MySQL 5.1 INTRODUCTION 5.2 TUTORIAL

SQL SELECT Query: Intermediate

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

Comparison of Open Source RDBMS

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

Why MySQL beats MongoDB

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

Overview of Databases On MacOS. Karl Kuehn Automation Engineer RethinkDB

DBMS / Business Intelligence, SQL Server

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

SQL 2: GETTING INFORMATION INTO A DATABASE. MIS2502 Data Analytics

ODBC Driver Version 4 Manual

SQL Server 2008 Core Skills. Gary Young 2011

Transcription:

Recap: the SQL Select Command SQL: joins SELECT [DISTINCT] sel_expression [, sel_expression ] FROM table_references [WHERE condition] [GROUPBY column [,column ] [[HAVING condition]] [ORDER BY columns [ASC DESC]] [LIMIT num]; 2 Recap: Tables for Plug-in Cars STATE SALES ST NAME POP COLOR MI CA CALIFORNIA 38 BLUE 57 ST MODEL QTY HI HAWAII 1 BLUE 56 WA LEAF 99 WA WASHINGTON 7 BLUE 62 WA I3 32 GA GEORGIA 10 RED 48 HI LEAF 26 OR OREGON 4 BLUE 52 CA LEAF 578 UT UTAH 3 RED 58 CA I3 186 CO COLORADO 5 PURPLE 57 CA S60 18 CAR CA SPARKEV 15 MODEL MAN PRICE BATTERY MOTORKW MPGE MILES WEIGHT CA 500E 30 I3 BMW 42375 22 125 123 80 2700 GA LEAF 118 BCE MB 42375 28 130 84 80 3935 GA I3 38 SPARKEV CHEVY 27495 20 110 119 82 2989 OR LEAF 39 LEAF NISSAN 31820 24 80 115 75 3340 OR I3 13 S60 TESLA 71070 60 222 89 208 4647 UT LEAF 12 500E FIAT 32650 24 83 116 87 2980 CO LEAF 25 Practices How many cars are sold (in total) at each state? What are the top 3 states? SELECT st, SUM(qty) FROM sales GROUP BY st ORDER BY SUM(qty) DESC LIMIT 3;

A shorthand can use numbers instead of column names as a shorthand the number corresponds to the order that appears in the SELECT clause These two queries are equivalent: SELECT st, SUM(qty) FROM sales GROUP BY st ORDER BY SUM(qty) DESC LIMIT 3; SELECT st, SUM(qty) FROM sales GROUP BY 1 ORDER BY 2 DESC LIMIT 3; Practices What states have sales of at least two different models of cars? SELECT st FROM sales GROUP BY st HAVING COUNT(*) >= 2 ORDER BY SUM(qty) DESC; Practices filter out duplicate emails SELECT DISTINCT Email FROM Person SELECT Email FROM Person GROUP BY Email HAVING COUNT(*) = 1 Person Id Email 1 a@b.com 2 c@d.com 3 a@b.com Return: Email a@b.com c@d.com Conditional logic CASE WHEN THEN [ELSE ] END SQL s way of handling if/else This statement defines a new column

Conditional logic: example Conditional logic: example SELECT model, man, price, FROM car CASE WHEN price > '35000' THEN 'no' ELSE 'yes' END AS can_afford I want to have another column that identifies whether I can afford that car multiple outcomes are evaluated from left to right can specify null values as well the ELSE is optional here SELECT model, man, price, CASE WHEN price > '50000' THEN 'over 50000' WHEN price > '35000' THEN 'over 35000' ELSE NULL END AS price_range FROM car Conditional logic: example CASE and aggregations SELECT FROM car GROUP BY 1 CASE WHEN price > '50000' THEN 'over 50000' WHEN price > '35000' THEN 'over 35000' ELSE 'below 35000' END AS price_range, COUNT(1) AS count price_range below 35000 3 over 35000 2 over 50000 1 count Conditional logic: example Pivoting the table display counts horizontally Note the case conditions! SELECT COUNT(CASE WHEN price > '50000' THEN 1 END) AS gt_50000_count, COUNT(CASE WHEN price > '35000' AND price < '50000' THEN 1 END) AS gt_35000_count, COUNT(CASE WHEN price < '35000' THEN 1 END) AS lt_35000_count, COUNT(*) AS total FROM car gt_50000_count gt_35000_count lt_35000_count total 1 2 3 6

Administrivia Blackboard up and running check out the slides let me know if you re not on Blackboard Slides also available at http://www.cs.rochester.edu/~yjin18/ Joins: extracting data from multiple relations CAR: 8 attributes, 6 records MODEL MAN PRICE BATTERY MOTORKW MPGE MILES WEIGHT I3 BMW 42375 22 125 123 80 2700 BCE MB 42375 28 130 84 80 3935 SPARKEV CHEVY 27495 20 110 119 82 2989 LEAF NISSAN 31820 24 80 115 75 3340 S60 TESLA 71070 60 222 89 208 4647 500E FIAT 32650 24 83 116 87 2980 SALES: 3 attributes, 14 records ST MODEL QTY WA LEAF 99 WA I3 32 HI LEAF 26 CA LEAF 578 CA I3 186 CA S60 18 CA SPARKEV 15 CA 500E 30 GA LEAF 118 GA I3 38 OR LEAF 39 OR I3 13 UT LEAF 12 CO LEAF 25 Join clarification: joins stitches data together horizontally after the join, the table is wider than before to be able to combine two table schemas, we ll have to find something in common SELECT (column_list) FROM table_name [INNER {LEFT RIGHT} {OUTER}] JOIN table_name ON qualification_list WHERE Join: an example two equivalent ways to write a join expression select * from car, sales where car.model = sales.model; select * from car join sales on car.model = sales.model;

Join all rows from the car table on to rows in the sales table, for which the model field in the car table is equal to the model field in the sales table 11 attributes, 14 records Types of joins inner join the default JOIN or INNER JOIN left (outer) join LEFT JOIN or LEFT OUTER JOIN right (outer) join RIGHT JOIN or RIGHT OUTER JOIN full (outer) join not implemented/available in MySQL http://joins.spathon.com/ Join: multiple tables For each sale, find the full names of states and manufacturer of cars, and the number sold in descending order select name, man, qty from state join sales join car on state.st = sales.st and sales.model = car.model order by qty desc; Practices Retrieve FirstName, LastName, City, and State for each person, regardless of whether that person has an address. Column Name PersonId FirstName LastName Person Type int varchar varchar Column Name AddressId PersonId City State Address Type int int varchar varchar

Practices Retrieve FirstName, LastName, City, and State for each person, regardless of whether that person has an address SELECT FirstName, LastName, City, State FROM Person LEFT JOIN Address ON Person.personId = Address.personId Practices Find customers who never order anything. SELECT C.Name from Customers C LEFT JOIN Orders O ON C.Id = O.CustomerId WHERE O.CustomerId is NULL Customers Id Name 1 Joe 2 Henry 3 Sam 4 Max Id Orders 1 3 2 1 CustomerId Different flavors of SQL Multiple standard revisions and multiple flavors/ implementations) exist Revisions of standard: successively introduced additional features, mostly backwards-compatible with older standards Common implementations: Oracle, MySQL, SQLite, Postgres, MSQL (Microsoft), Microsoft Access Implementations comply with the standard, but people almost never completely implement everything in the standard Market share: Source: JoinVision E-Services GmbH, July 2006

UNIONs stack a dataset on top of another Restrictions Both tables must have the same number of columns The columns must have the same data types in the same order as the first table UNION appends distinct values UNION ALL appends all values (probably used more often) UNIONs: example can simulate a FULL OUTER JOIN using unions example: the User and Likes database SELECT users.name, likes.like FROM users LEFT OUTER JOIN likes ON users.id = likes.user_id UNION ALL SELECT users.name, likes.like FROM users RIGHT OUTER JOIN likes ON users.id = likes.user_id JOIN tricks self joins Employee Id Name Salary ManagerId 1 Joe 70000 3 2 Henry 80000 4 3 Sam 60000 NULL 4 Max 90000 NULL Find people who earn more than their managers. In our example the query should return Employee Joe JOIN tricks self joins Find people who earn more than their managers. SELECT employee.name AS 'Employee' FROM Employee employee JOIN Employee manager ON employee.managerid = manager.id WHERE employee.salary > manager.salary Id Name Salary ManagerId 1 Joe 70000 3 2 Henry 80000 4 3 Sam 60000 NULL 4 Max 90000 NULL

JOIN tricks inequality joins find pairs of car model names, in which the first name is shorter than the next SELECT car1.model, car2.model FROM car AS car1 JOIN car AS car2 ON length(car1.model) < length(car2.model) JOIN tricks multiple keys SELECT * FROM car JOIN sales ON car.model = sales.model AND car.man = sales.man MODEL MAN PRICE BATTERY MPGE MILES WEIGHT I3 BMW 42375 22 123 80 2700 BCE MB 42375 28 84 80 3935 SPARKEV CHEVY 27495 20 119 82 2989 LEAF NISSAN 31820 24 115 75 3340 S60 TESLA 71070 60 89 208 4647 500E FIAT 32650 24 116 87 2980 ST MODEL MAN QTY WA LEAF NISSAN 99 WA I3 BMW 32 HI LEAF NISSAN 26 CA LEAF NISSAN 578 CA I3 BMW 186 CA S60 TESLA 18 CA SPARKEV CHEVY 15 CA 500E FIAT 30 GA LEAF NISSAN 118 GA I3 BMW 38 OR LEAF NISSAN 39 OR I3 BMW 13 UT LEAF NISSAN 12 CO LEAF NISSAN 25 JOIN tricks multiple keys Why? accuracy speed has to do with implementations indexes might hit one of the indexes and get a better speed Datatypes why datatypes? easier for data manipulation easier to store, and saves space on disk! depending on the implementation, datatypes vary, but more or less the same functions usually act on just one type of data, e.g. SUM() for numerical data in MySQL 5.7, there are 5 major categories (numeric, text, date/time, spatial, JSON) each has a handful of datatypes

Datatypes: numeric Integer Types (Exact Value): INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT Fixed-Point Types (Exact Value): DECIMAL, NUMERIC Floating-Point Types (Approximate Value): FLOAT, DOUBLE Bit-Value Type: BIT the difference is in the size. e.g. TINYINT ranges from -128 to 127, while INT ranges from -2147483648 to 2147483647 Datatypes: string CHAR, VARCHAR fixed and variable length string BINARY, VARBINARY fixed and variable length binary BLOB (Binary Large OBjects), TEXT binary and character of bigger size ENUM a list of possible values (e.g. XS, S, M, L, XL ) SET like enum but stores more than 1 choice Datatypes: date and time DATE YYYY-MM-DD DATETIME YYYY-MM-DD HH:MI:SS TIMESTAMP Also YYYY-MM-DD HH:MI:SS, but stored as the number of seconds since '1970-01-01 00:00:00' UTC (i.e. the Unix epoch) TIME HH:MI:SS YEAR A year in two-digit or four-digit format. Datatypes: date and time Date and time are tricky to get right! How do you compare dates? take differences? group into weeks or months? convert string to dates?

Datatypes: others Spatial GEOMETRY, POINT, LINESTRING, POLYGON etc JSON See for yourself! Reference at http:// dev.mysql.com/doc/refman/5.7/en/datatypes.html More resources: learning SQLSchool: interactive PostgreSQL tutorial with examples https://sqlschool.modeanalytics.com/ SQLZoo: interactive SQL tutorial with exercises http://sqlzoo.net/ W3Schools: http://www.w3schools.com/sql/ More resources: troubleshooting Googling The database s reference manual e.g. MySQL 5.7 Reference Manual is at http://dev.mysql.com/doc/refman/5.7/en/ Ask questions at http://stackoverflow.com/ More resources: data http://alpha.data.gov/ A collection of open data from government, companies, and nonprofits including: Commerce - GPS, Weather, Census Data Energy - Outlook and Analysis, Energy Usage, Energy Star, Your Car s Data Stream, Renewable Energy + See all energy datasets and tools Health - Recalls and Safety Alerts, Medicare Plans, Hospital Quality Data, Public Health Data, Clinical Trials, Health Indicators, Your Health Record + See all health datasets & tools Education - Open Badges, Academic Data, Learning Registry, Education Data Standards, Higher Education Datasets + See all education datasets & tools 40

Health Data for Epidemiology http://chir.asu.edu/data A key activity of CHiR from its outset has been obtaining health data being collected routinely by various health care and public health entities and making these data available for use for Data for Urban Planning http://www.cityforward.org/ City Forward data catalog is a listing of the data sets currently available for use from publicly available data sources about cities and metropolitan areas around the world. Please use the links back to the data source and their terms of use. 41 42 Database of Technology Companies http://www.crunchbase.com/ CrunchBase is the free database of technology companies, people, and investors that anyone can edit. Google DB Search Engine www.google.com/publicdata/directory Useful search engine You can save to my data Google maintains over 100 million curated pages of data 43 44

Amazon Public Data Sets http://aws.amazon.com/datasets AWS Amazon Web Services Hadoop friendly files Can use seamlessly with Amazon cloud computing utility EC2 Amazon Elastic Cloud Compute In conclusion Can use conditional logic to create new columns and pivot tables. There are 4 types of joins to integrate information from two tables. UNION and UNION ALL stitches two parts of the dataset together. Common data types include numerics, strings and date/times. 45 Stay tuned for next week subqueries window functions (maybe) query optimization and the week after next inserting, deleting and updating records constraints for tables what else do you want to see?