Database Queries aka SQL (pronounced “ sequel ” ).

Slides:



Advertisements
Similar presentations
Query Methods (SQL). What is SQL A programming language for databases. SQL (structured Query Language) It allows you add, edit, delete and run queries.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 2 Structured Query Language.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
Getting Started (Excerpts) Chapter One DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
CS162 Discussion Notes March 31, 2011 Jorge Ortiz.
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation Chapter Two: Introduction to Structured Query Language.
Mary K. Olson PS Reporting Instance – Query Tool 101.
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Structured Query Language
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1 Created By Martin Schedlbauer
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
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.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Two: Introduction to Structured Query Language.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 11-1 David M. Kroenke’s Chapter Eleven: Managing Databases with SQL Server.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
BY SATHISH SQL Basic. Introduction The language Structured English Query Language (SEQUEL) was developed by IBM Corporation, Inc., to use Codd's model.
Getting Started Chapter One DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
Chapter 9 Structured Query Language David M. Kroenke Database Processing © 2000 Prentice Hall.
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.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Database UpdatestMyn1 Database Updates SQL is a complete data manipulation language that can be used for modifying the data in the database as well as.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Assoc. Prof. Dr. Ahmet Turan ÖZCERİT.  Basic SQL syntax  Data retrieve  Data query  Data conditions  Arithmetic operations on data  Data transactions.
Structured Query Language
DAVID M. KROENKE’S DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Two: Introduction to Structured Query Language.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
1 Structured Query Language (SQL) Pertemuan 09 Matakuliah: F0712 / Lab MS Access Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
CSIS 115 Database Design and Applications for Business Dr. Meg Fryling “Dr. Meg” Fall #csis115 © 2012 Meg Fryling.
CSIS 115 Database Design and Applications for Business
Query Methods Where Clauses Start ….
Introduction to Structured Query Language(SQL)
COS 346 Day 3 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
Structured Query Language (SQL) William Klingelsmith
Database Principles Constructed by Hanh Pham based on slides from: “Database Processing, Fundamentals, Design, and Implementation”, D. Kroenke, D. Auer,
Access: SQL Participation Project
Contents Preface I Introduction Lesson Objectives I-2
Topic 12 Lesson 2 – Retrieving Data with Queries
Manipulating Data Lesson 3.
Presentation transcript:

Database Queries aka SQL (pronounced “ sequel ” )

What is SQL? SQL = Standard Query Language SQL = Standard Query Language SQL is a language for retrieving, creating, modifying, or removing data from just about any modern database SQL is a language for retrieving, creating, modifying, or removing data from just about any modern database

What is SQL? To get information from a database file, a user issues a “query” To get information from a database file, a user issues a “query” A “query” is a request to retrieve data A “query” is a request to retrieve data MS Access will go through all the records in the database and “select” those records that satisfy the search condition MS Access will go through all the records in the database and “select” those records that satisfy the search condition SQL (Structured Query Language) allows a user to specify the conditions without writing complex code SQL (Structured Query Language) allows a user to specify the conditions without writing complex code

Basic SQL Commands Creating tables with CREATE Creating tables with CREATE Adding data with INSERT Adding data with INSERT Viewing data with SELECT Viewing data with SELECT Removing data with DELETE Removing data with DELETE Modifying data with UPDATE Modifying data with UPDATE Destroying tables with DROP Destroying tables with DROP

Retrieving Data from One Table

The SELECT SELECT column1, column2, … FROM tablename {WHERE condition } {ORDER BY column }

A few simple SELECTs SELECT * FROM contacts; SELECT * FROM contacts; –Display all records in the ‘contacts’ table SELECT contactid, name FROM contacts; SELECT contactid, name FROM contacts; –Display only the contactid and name SELECT contactname FROM contacts ORDER BY contactname; SELECT contactname FROM contacts ORDER BY contactname; –Display only the contactname, sort by name

The SELECT SELECT* FROMTABLE; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall ALL Columns No WHERE Clause so ALL Rows are Returned End of Command

Refining selections with WHERE The WHERE “clause” allows you to select records based on a condition The WHERE “clause” allows you to select records based on a condition SELECT * FROM contacts WHERE age<10; SELECT * FROM contacts WHERE age<10; –Display records from contacts where age<10 SELECT * FROM contacts WHERE age BETWEEN 18 AND 35; SELECT * FROM contacts WHERE age BETWEEN 18 AND 35; –Display records where age is 18-35

WHERE with “AND” SELECT* FROMSALES_DATA WHEREDepartment = 'Water Sports' ANDBuyer = 'Nancy Meyers'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE with “OR” SELECT* FROMSALES_DATA WHEREDepartment = 'Camping' ORDepartment = 'Climbing'; ORDepartment = 'Climbing'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE with “IN” SELECT* FROMSALES_DATA WHEREBuyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE with “BETWEEN” SELECT* FROMORDERS WHEREExtendedPrice BETWEEN 100 AND 200; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE with “LIKE” SELECT* FROMSALES_DATA WHEREBuyer LIKE 'Pete*'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

WHERE with “LIKE” SELECT* FROMSALES_DATA WHERESKU_Description LIKE ‘*Tent*'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Time for Hands-On Photo © Charles Darwin University 2005

SQL Built-in Functions There are five SQL Built-in Functions: There are five SQL Built-in Functions: –COUNT –SUM –AVG –MIN –MAX Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Built-in Functions SELECTSUM (ExtendedPrice) ASOrder3000Sum FROMORDERS WHEREOrderNumber = 3000; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall Column Heading

SQL Built-in Functions (Continued) SELECTSUM (ExtendedPrice) AS OrderItemSum, AVG (ExtendedPrice) AS OrderItemAvg, MIN (ExtendedPrice) AS OrderItemMin, MAX (ExtendedPrice) AS OrderItemMax FROMORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SQL Built-in Functions (Continued) SELECTCOUNT(*) AS NumRows FROMORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Arithmetic in SELECT Statements SELECTQuantity * Price AS EP, ExtendedPrice FROMORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Time for Hands-On Photo © Charles Darwin University 2005

Retrieving Data from More Than One Table

Make a list of students including phone number and address Make a list of students including phone number and address Join Person Phone Address

Joining together tables Person PersonIDNameAddressID 1Joe10 2Jane20 3Chris30 AddressAddressIDCompanyStreetZip 10ABC 12 Road XYZ 45 Road PDQ 78 Road PhonePhoneIDPersonIDPhoneNum

The SELECT with a JOIN Clause SELECTTABLE1.Column, TABLE2.Column TABLE2.Column FROMTABLE1, TABLE2 WHERETABLE1.PrimaryKey = TABLE2.PrimaryKey; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

The SELECT with a JOIN Clause SELECTPERSON.Name, PHONE.PhoneNum FROMPERSON, PHONE WHEREPERSON.PersonID = PHONE.PersonID; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

The SELECT with two JOIN Clauses SELECTPERSON.Name, PHONE.PhoneNum,ADDRESS.Zip FROMPERSON, PHONE, ADDRESS WHEREPERSON.PersonID = PHONE.PersonID ANDPERSON.AddressID = ADDRESS.AddressID Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

SELECTP.Name, PH.PhoneNum FROMPERSON P, PHONE PH WHEREP.PersonID = PH.PersonID Alias (Shorthand) for Table Name The SELECT with a JOIN Clause

Time for Hands-On Photo © Charles Darwin University 2005

Writing SQL in MS Access 2007

Click “Create”

Click “Query Design”

Click “Close”

Click “SQL”

Click “ ! Run”