DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Similar documents
Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Lecture 2: Introduction to SQL

Big Data Processing Technologies. Chentao Wu Associate Professor Dept. of Computer Science and Engineering

Lecture 3: SQL Part II

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

SQL. Assit.Prof Dr. Anantakul Intarapadung

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

MTAT Introduction to Databases

COMP 430 Intro. to Database Systems

Introduction to Database Systems CSE 444

SQL. Structured Query Language

INF 212 ANALYSIS OF PROG. LANGS SQL AND SPREADSHEETS. Instructors: Crista Lopes Copyright Instructors.

Lectures 2&3: Introduction to SQL

Agenda. Discussion. Database/Relation/Tuple. Schema. Instance. CSE 444: Database Internals. Review Relational Model

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2006 Lecture 3 - Relational Model

Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

Principles of Database Systems CSE 544. Lecture #2 SQL The Complete Story

CMPT 354: Database System I. Lecture 3. SQL Basics

CSE 344 JANUARY 8 TH SQLITE AND JOINS

Operating systems fundamentals - B07

Introduction to Databases CSE 414. Lecture 2: Data Models

Before we talk about Big Data. Lets talk about not-so-big data

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

Introduction to Relational Databases

Announcements. Agenda. Database/Relation/Tuple. Discussion. Schema. CSE 444: Database Internals. Room change: Lab 1 part 1 is due on Monday

Announcements. Agenda. Database/Relation/Tuple. Schema. Discussion. CSE 444: Database Internals

L07: SQL: Advanced & Practice. CS3200 Database design (sp18 s2) 1/11/2018

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

Data Informatics. Seon Ho Kim, Ph.D.

CSC Web Programming. Introduction to SQL

Introduction to Data Management CSE 344. Lecture 2: Data Models

Lecture 3 Additional Slides. CSE 344, Winter 2014 Sudeepa Roy

CSE 544 Principles of Database Management Systems

CS 564 Midterm Review

SQL: Data Definition Language

CSE 544 Principles of Database Management Systems

The Relational Data Model

Keys, SQL, and Views CMPSCI 645

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

SQL DATA DEFINITION LANGUAGE

Introduction to Data Management CSE 414

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Introduction to Data Management CSE 344

Chapter 3: Introduction to SQL

Database Systems CSE 414

SQLite, Firefox, and our small IMDB movie database. CS3200 Database design (sp18 s2) Version 1/17/2018

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

CSC 261/461 Database Systems Lecture 5. Fall 2017

1) Introduction to SQL

SQL DATA DEFINITION LANGUAGE

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

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

Chapter 3: Introduction to SQL

SQL DATA DEFINITION LANGUAGE

Introduction to Database Systems. The Relational Data Model

Introduction to Database Systems. The Relational Data Model. Werner Nutt

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

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

Unit 1 - Chapter 4,5

user specifies what is wanted, not how to find it

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

CSE 344 Introduction to Data Management. Section 2: More SQL

Introduction to Data Management CSE 344

Lecture 04: SQL. Monday, April 2, 2007

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

SQL: The Query Language Part 1. Relational Query Languages

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

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

Introduction to Database Systems CSE 414

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

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

COMP 430 Intro. to Database Systems

Announcements. Multi-column Keys. Multi-column Keys (3) Multi-column Keys. Multi-column Keys (2) Introduction to Data Management CSE 414

CMPT 354: Database System I. Lecture 2. Relational Model

Announcements. Using Electronics in Class. Review. Staff Instructor: Alvin Cheung Office hour on Wednesdays, 1-2pm. Class Overview

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Basic SQL. Basic SQL. Basic SQL

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

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

Lecture 04: SQL. Wednesday, October 4, 2006

Oracle Database: SQL and PL/SQL Fundamentals NEW

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

Announcements. Multi-column Keys. Multi-column Keys. Multi-column Keys (3) Multi-column Keys (2) Introduction to Data Management CSE 414

The Entity-Relationship Model (ER Model) - Part 2

SQL Structured Query Language Introduction

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

Oracle Database 11g: SQL and PL/SQL Fundamentals

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

Announcements. Outline UNIQUE. (Inner) joins. (Inner) Joins. Database Systems CSE 414. WQ1 is posted to gradebook double check scores

SQL STRUCTURED QUERY LANGUAGE

QQ Group

The Relational Model

Transcription:

DS 1300 - Introduction to SQL Part 1 Single-Table Queries By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Overview 1. SQL introduction & schema definitions 2. Basic single-table queries 2

1. SQL INTRODUCTION & DEFINITIONS 3

What you will learn about in this section 1. What is SQL? 2. Basic schema definitions 3. Keys & constraints intro 4. Activities: CREATE TABLE statements 4

Basic SQL 5

SQL Introduction SQL stands for Structured Query Language SQL is a standard language for querying and manipulating data. SQL is a high-level, declarative programming language. SQL execution is highly optimized and parallelized. Many standards out there: Standardized in 1986/87 ANSI SQL/ SQL-86, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3), SQL:2011 Vendors support various subsets (e.g., SQLite implements most of the SQL-92 standard)

SQL is a Data Definition Language (DDL) Define relational schemata Create/alter/delete tables and their attributes Data Manipulation Language (DML) Insert/delete/modify tuples in tables Query one or more tables 7

Tables in SQL Product PName Price Manufacturer Gizmo $19.99 GizmoWorks Powergizmo $29.99 GizmoWorks SingleTouch $149.99 Canon MultiTouch $203.99 Hitachi A relation or table is a multiset of tuples having the attributes specified by the schema This is where the name relational databases comes from. 8

Tables in SQL Product PName Price Manufacturer Gizmo $19.99 GizmoWorks Powergizmo $29.99 GizmoWorks SingleTouch $149.99 Canon MultiTouch $203.99 Hitachi An attribute (or column) is a typed data entry present in each tuple in the relation Attributes must have an atomic type in standard SQL, i.e. not a list, set, etc. 10

Tables in SQL Product PName Price Manufacturer Gizmo $19.99 GizmoWorks Powergizmo $29.99 GizmoWorks SingleTouch $149.99 Canon MultiTouch $203.99 Hitachi A tuple or row is a single entry in the table having the attributes specified by the schema Sometimes also referred to as a record 11

Data Types in SQL Atomic types: Characters: CHAR(20), VARCHAR(50) Numbers: INT, BIGINT, SMALLINT, FLOAT Others: MONEY, DATETIME, SQLite uses: integer, text and real Every attribute must have an atomic type Why? 13

Table Schemas The schema of a table is the table name, its attributes, and their types: Product(Pname: text, Price: real, Category: text, Manufacturer: text) A key is an attribute (combination) that identifies a tuple uniquely. Product(Pname: text, Price: real, Category: text, Manufacturer: text) 14

Key constraints A key is a minimal subset of attributes that acts as a unique identifier for tuples in a relation A key is an implicit constraint on which tuples can be in the relation i.e., if two tuples agree on the values of the key, then they must be the same tuple! Students(sid: name: gpa: text, text, real) 1. Which would you select as a key? 2. Is a key always guaranteed to exist? 3. Can we have more than one key? (key candidates and primary key)

NULL and NOT NULL To say don t know the value we use NULL Students(sid:text, name:text, gpa: real) sid name gpa 123 Bob 3.9 143 Jim NULL Say, Jim just enrolled in his first class. In SQL, we may constrain a column to be NOT NULL, e.g., name in this table

Activities SQLite data types: http://www.tutorialspoint.com/sqlite DB Browser Create a database Create a Product table Add the shown data PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi 19

2. SINGLE-TABLE QUERIES 20

What you will learn about in this section 1. The SFW query 2. Other useful operators: LIKE, DISTINCT, ORDER BY 3. Activities: Single-table queries 21

SQL Query Basic form (there are many many more bells and whistles) SELECT <attributes> FROM <one or more relations> WHERE <conditions> Call this a SFW query. 22

Simple SQL Query: Selection Selection is the operation of filtering a relation s tuples on some condition SELECT * FROM Product WHERE Category = Gadgets PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks 23

Simple SQL Query: Projection Projection is the operation of producing an output table with tuples that have a subset of their prior attributes PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi SELECT Pname, Price, Manufacturer FROM Product WHERE Category = Gadgets PName Price Manufacturer Gizmo $19.99 GizmoWorks Powergizmo $29.99 GizmoWorks 24

Notation Input schema Product(PName, Price, Category, Manfacturer) SELECT Pname, Price, Manufacturer FROM Product WHERE Category = Gadgets Output schema Answer(PName, Price, Manfacturer) 25

A Few Details SQL commands are case insensitive: Same: SELECT, Select, select Same: Product, product Values are not: Different: Seattle, seattle Use single quotes for text constants: abc - yes abc - no 26

DISTINCT: Eliminating Duplicates SELECT DISTINCT Category FROM Product Versus SELECT Category FROM Product Category Gadgets Photography Household Category Gadgets Gadgets Photography Household 27

COUNT COUNT is an aggregation function that returns the number of elements. Example: Find the number of products with a price of $20 or more. SELECT COUNT(*) FROM product WHERE price >= 20 Syntax: COUNT([ALL DISTINCT] expression) 28

ORDER BY: Sorting the Results SELECT PName, Price, Manufacturer FROM Product WHERE Category= gizmo AND Price > 50 ORDER BY Price, PName Ties are broken by the second attribute on the ORDER BY list, etc. Ordering is ascending, unless you specify the DESC keyword. Text is ordered alphabetically. 29

LIMIT Clause Used to limit the data amount returned by the SELECT statement. Example: Find the 5 most expensive products SELECT * FROM product ORDER BY price DESC LIMIT 5 Syntax: LIMIT [no of rows] OFFSET [row num] Note: LIMIT is not standard SQL (e.g., MS SQL Server uses SELECT TOP) 30

Operators Some of the operators supported by SQL are: =, == equal!=, <> not equal <, <= less than (or equal) >, >= greater than (or equal) +, -, /, * arithmetic operators AND, OR logic operators Example: Find products and their price + 8% sales tax for gadgets that cost at least $100 SELECT pname, price * 1.08 AS Price_with_tax FROM product, WHERE category = Gadgets AND price >= 100 31

IN and BETWEEN The IN operator allows you to specify multiple values in a WHERE clause. SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...) The BETWEEN operator selects values within a range. The values can be numbers, text, or dates. SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2 32

LIKE: Simple String Pattern Matching SELECT * FROM Products WHERE PName LIKE %gizmo% s LIKE p: pattern matching on strings p may contain two special symbols: % = any sequence of characters _ = any single character 33

CASE Statement CASE WHEN [condition1] THEN [expression1] WHEN [condition2] THEN [expression2] ELSE [default expression] END Product name category price Gizmo gadget 50 Camera Photo 299 OneClick Photo 89 Example: SELECT name, CASE WHEN price > 200 THEN Yes ELSE No END AS expensive FROM Product 34

Activities SQLite Operators Expressions Where clauses And & Or clauses (http://www.tutorialspoint.com/sqlite/) 1. Find all the gadgets and sort them by price. 2. What is the most expensive gadget? 3. How many gadgets are in the database? 4. How many gadgets are less than $20? 5. How much does it cost to buy all gadgets? 6. What happens if the manufacturer GizmoWorks changes its name? This is why we need multiple tables! 35