Structured Query Language (SQL)

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Advanced SQL Topics Edward Wu.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Information Retrieval from Relational Databases
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Copyright © 2006 by The McGraw-Hill Companies,
Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Relational data objects 1 Lecture 6. Relational data objects 2 Answer to last lectures activity.
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
SQL: The Query Language Part 2
Data Structures: A Pseudocode Approach with C
ABC Technology Project
Chapter 7 Working with Databases and MySQL
Introduction to Structured Query Language (SQL)
Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.
1 Web-Enabled Decision Support Systems Access Introduction: Touring Access Prof. Name Position (123) University Name.
Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Yong Choi School of Business CSU, Bakersfield
Microsoft Access.
State of Connecticut Core-CT Project Query 8 hrs Updated 6/06/2006.
Benchmark Series Microsoft Excel 2013 Level 2
Sets Sets © 2005 Richard A. Medeiros next Patterns.
Chapter 5 Test Review Sections 5-1 through 5-4.
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
Computer Concepts BASICS 4th Edition
Benchmark Series Microsoft Excel 2010 Level 1
Exploring Microsoft Access
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Concepts of Database Management, 4th Edition, Pratt & Adamski
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
Concepts of Database Management Seventh Edition
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
How to: SQL By: Sam Loch.
Chapter 5 Introduction to SQL.
 2012 Pearson Education, Inc. All rights reserved.
The Database Exercises Fall, 2009.
Presentation transcript:

Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27th 2005

Overview SQL Language (SQL 92) Create Table Select All examples can be used with the “AmazonBook” database This database can be downloaded from the “Course Material” section in WebCT

Introduction/Review What is SQL Some facts about SQL Review: Database and DBMS (chapter 1) SQL is a standard language accepted by relational DBMS to perform database operations Some facts about SQL SQL 92 is the most commonly supported version English-like (not programming) Case insensitive Venders have different implementations

Concepts Review Relational model concepts review Relation : Table Row : Record Column : Field (Attribute)

Create Table Statement CREATE TABLE TableName ( Column 1 Definition, Column 2 Definition, …, PRIMARY KEY (Column, [Column]) )

Column Definition Column Definition Format Example ColumnName DataType BookTitle Text(200) NumberofPages Integer ListPrice Currency

Common Data Types Numeric Character/String Date INTEGER, SMALLINT DECIMAL(i,j), NUMBER(i,j) Character/String CHAR(n), VARCHAR(n) Date DATE, DATETIME See textbook page 42-43 for data types implemented in Access

Create Table Example CREATE TABLE Books ( Title VARCHAR(100), Publisher VARCHAR(50), ISBN CHAR(13), Price DECIMAL(6,2), Year INTEGER, PRIMARY KEY (ISBN) ) Exercise: understand the statement What is the name of this table? How many columns are there? Which one is the primary key? How many characters are allowed for a book title? What is the maximum price that can be entered for any book?

SELECT Statement SELECT statement retrieves data from database (query) The result is usually another table We will learn Selection and projection Sorting Calculation Demo all examples in Access

SELECT Statement Syntax SELECT Column(s) or other expressions FROM Table(s) [WHERE …] [ORDER BY ColumnName] The order of the keyword is important

Simple SELECT Statement Syntax SELECT * (or a list of columns) FROM TableName Wild card: * Example SELECT * FROM Books SELECT BookTitle, ListPrice FROM Books

Use WHERE Clause Use WHERE clause to specify selection criteria Example SELECT * FROM Books WHERE ListPrice = 29.99 SELECT BookTitle, ListPrice FROM Books WHERE ListPrice < 20 Comparison Operators “=“, “>”, “<“, “>=“, “<=“, “<>”

More Comparison Operators IN (value list) SELECT * FROM Books WHERE ListPrice IN (19.99, 29.99, 39.99) BETWEEN min AND max WHERE ListPrice BETWEEN 9.99 AND 19.99

String Pattern Match Fuzzy query using LIKE Example _ (underscore): single character wildcard ? in Access % (percentage): multiple character wildcard * in Access Example SELECT * FROM Books WHERE BookTitle LIKE '*information systems*'

Compound Conditions Use logical operators to connect multiple conditions AND: an intersection of the data sets OR: a union of the data sets Examples SELECT * FROM Books WHERE ListPrice <= 19.99 AND ListPrice >= 9.99 WHERE PubDate=#10/1/2003# OR PubDate=#10/1/2004# SELECT * from Books WHERE Publisher = 'Que' AND Binding = 'Paperback'

Sorting Syntax Examples ORDER BY Column(s) [ASC/DESC] SELECT * FROM Books ORDER BY PubDate ORDER BY Publisher DESC, PubDate

Calculation Calculating columns Example Calculated columns are not designed directly into the table Using +, -, *, / with columns and numbers Example SELECT BookTitle, ListPrice, ListPrice * 0.15 AS Discount FROM Books WHERE ListPrice * 0.15 > 20 Usage example: Order: unitprice*quantity

Built-in Functions Using these functions to do statistics Example MIN MAX COUNT AVG SUM Example SELECT COUNT(*) FROM Books SELECT AVG(ListPrice) FROM Books WHERE Publisher = 'Prentice Hall'

[Grouping] GROUP BY: doing math with groups SELECT COUNT(*) FROM Books WHERE Publisher = 'Prentice Hall'; SELECT COUNT(*) FROM Books WHERE Publisher = 'The MIT Press'; … Or: SELECT Publisher, COUNT(*) FROM Books GROUP BY Publisher

A Complete Query SELECT ISBN, BookTitle, ListPrice, Publisher FROM Books WHERE BookTitle like '*Information Systems*' AND PubDate > #1/1/2002# AND ListPrice < 100 ORDER BY ListPrice

Exercise Using the “AmazonBook” database, use SQL or QBE to answer the following questions. Which book is the most expensive? How many books are under $100 of list price? I am looking for a “database” book, no more than 50 dollars, and published after 1/1/2003; do you have any recommendations? I need the book title and its price. What is the price per page for the books published by Que? List the book title, price, number of pages and price for page, and sort by the price per page, descending Write out the potential results of these SQL statements

Updating Tables INSERT INTO UPDATE DELETE FROM Read this part yourself To add a new record UPDATE To change data DELETE FROM To delete records Read this part yourself

Summary SQL is a standard Relational DBMS language SQL statements CREATE TABLE SELECT WHERE clause DELETE FROM INSERT INTO UPDATE

Good Resources SQL Online Tutorial http://sqlcourse.com/ http://sqlcourse2.com/