# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
This course has taken from This unique introductory SQL tutorial not only provides easy-to-understand SQL instructions, but it allows.
1 CSE Students: Please do not log in yet. Check-in with Brian in the back. Review Days 3 and 4 in the book. Others: Please save your work and logout.
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
 The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data retrieved.  Comparison Operators Comparison OperatorDefinition.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
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.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Exam Review – Queries & MORE! Access SimNet Exam Access Case Exam Final Exam.
SQL Review Tonga Institute of Higher Education. SQL Introduction SQL (Structured Query Language) a language that allows a developer to work with data.
Microsoft Access 2010 Building and Using Queries.
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
Database: SQL and MySQL
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL (DDL & DML Commands)
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Database Systems Microsoft Access Practical #3 Queries Nos 215.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Ch Determine the output displayed from procedures Lab sheet 10.4.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Microsoft Office XP Illustrated Introductory, Enhanced Tables and Queries Using.
Database Applications – Microsoft Access Lesson 4 Working with Queries 36 Slides in Presentation.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Fall Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday,
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Prepared By: Bobby Wan Microsoft Access Prepared By: Bobby Wan
Microsoft Office Access 2010 Lab 3
Chapter 5 Introduction to SQL.
Writing Basic SQL SELECT Statements
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
 2012 Pearson Education, Inc. All rights reserved.
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
ATS Application Programming: Java Programming
Building and Using Queries
Writing Basic SQL SELECT Statements
Structured Query Language (SQL) William Klingelsmith
CIS16 Application Programming with Visual Basic
Writing Basic SQL SELECT Statements
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105

# 2# 2 What Is a Query? A query is a question you ask of your database. You can: –display data from multiple tables –control which fields display –perform calculations on field values –save a query automatically

# 3# 3 Spring 2010 CS105 Sample query Note we have to capitalize the table names

# 4# 4 Spring 2010 CS105 Select Query also called a Result Set The results of a query are displayed as a result set (NOT SAVED AUTOMATICALLY, COULD CHANGE THE NEXT TIME IT IS RUN)—called recordset in some programming situations.

# 5# 5 Spring 2010 CS105 Select query-- most common query Retrieves and displays specific data requested from one or more tables; can specify display order—let’s try some: Action queries do mass record updates in one operation. Types: Update Query: alter the data in a Table Append Query: adds records from one table to another table Delete Query: deletes certain records, for example <1980 Make Table Query: creates a new Table from a query’s results. Query Types & Descriptions

# 6# 6 Spring 2010 CS105  Select * from Customers Select, *, and from are reserved (key) words and symbols that have special meaning in SQL SQLyog displays key words and symbols in blue. After the word from, if SQL sees the word Customers, it assumes it's a name of a table (see appendix E for a complete list of reserved or key words). Reserved or Key Words

# 7# 7 Spring 2010 CS105 Strings where cust_name = “Village Toys”  After the key word where, SQL assumes that the word cust_name is the name of a field (column) “Village Toys” is a string  A string is a sequence of ASCII symbols surrounded by single or double quotes that denotes a value (piece of information)

# 8# 8 Spring 2010 CS105 Wildcards The percent sign "%" can be used as a wild card to match any possible character that might appear before or after the characters specified. In another database, the wildcard might be different (such as *) select FirstName, Lastname, City from Employee where FirstName LIKE “ Mi% “; This SQL statement will match any first names that start with ‘Mi'..

# 9# 9 Spring 2010 CS105 More Wildcard Characters Lesson 6 in Sams Underscore: _ means that a character must be present Like ‘_oat’ returns boat but not oat Note: From or from both work fine

# 10 Spring 2010 CS105 Logical operators AND condition (happy only with both) OR condition (happy with either one) NOT condition (happy with anything but this one) For NOT, must be NOT (name = ‘smith’) select * from Pets where not( name = "Hortence“ )

# 11 Spring 2010 CS105 Relational Criteria, as in Where LastName = “Smith” See Lesson 4 in SAMS book = > < (equal to, greater than, less than) = Not equal to LIKE Pattern matching operator BETWEEN IS NULL IN

# 12 Spring 2010 CS105 Examples of Relational Query Criteria LIKE ‘Smith’ Age between 21 and 65 (Age >= 21 And Age <= 65 ) <= returns values of less than or equal to Not (firstname= ‘Smith’) Select records with values other than Smith

# 13 Spring 2010 CS105 Example: Do not do this!!!! Note: Repeat field firstname each time because—this is the worst part—the Query will RUN but the results will be WRONG! Select * from Customers where firstname = "Harry“ or “Fay”

# 14 Spring 2010 CS105 Order of precedence: When more than one logical operator is used in a query, NOT is evaluated first, then AND, and finally OR To make sure the statement is read the way you want it to be, use parentheses, because any statement within parentheses is evaluated first!

# 15 Spring 2010 CS105 select * from Movies where Rating 8.5 and year > 2000 select * from Movies where (Rating 8.5) and year > 2000 Example:

# 16 IN takes a comma delimited list of values, select * from Movies where Language IN ('Hindi','Mandarin') which is equivalent to select * from Movies where Language ='Hindi‘ OR Language = Mandarin' Spring 2010 CS105 IN Operator

# 17 select title select title from Movies from Movies where ID IN ( select MovieID where ID IN ( select MovieID from Casting from Casting where ActorID = 3) where ActorID = 3) Spring 2010 CS105 IN Operator to perform subqueries IN Operator to perform subqueries List all movies that Julia Roberts (ActorID = 3) performed. Or select title select title from Movies from Movies where ID IN ( select MovieID where ID IN ( select MovieID from Casting from Casting where ActorID = (select ID where ActorID = (select ID from Actors from Actors where LastName = 'Roberts')) where LastName = 'Roberts'))

# 18 Spring 2010 CS105 Multiple Tables Two tables can be linked by a common field Why would we use several tables rather than one big one? How can you link one table to another? Customers Billing

# 19 Spring 2010 CS105 Tables relating to other tables via fields

# 20 Spring 2010 CS105 Join using Where -- Lesson 13, in SAMs When you are looking for data from two tables, you want to limit your “hits” to records that match You relate one table to another by a common field You want a single set of output is returned, and the join associates the correct rows in each table on the fly Oops—what does “on the fly” mean? (not a permanent relationship)

# 21 Spring 2010 CS105 A simple WHERE join You do not need to specify Products.prod_name because Vendors does not have a field named prod_name

# 22 Spring 2010 CS105 What happens if you don’t use the Where join? Every vendor is listed with every product, so total: 9 * 6 = 54 records (!!!) Case Sensitive For Table Names

# 23 Spring 2010 CS105 You get too many hits!!

# 24 Spring 2010 CS105 Order of statements in a Select Query See Lesson 5 in Sams The Where clause comes after the From clause The Order By clause must be last, or you will get an error message

# 25 Spring 2010 CS105 Fields that you show aren’t necessarily the ones that you use for the join or filter…

# 26 Spring 2010 CS105 Note: if field names are duplicated in various tables, refer to fields specifically (note the table that they are from)

# 27 Spring 2010 CS105 In ascending order, NULL fields are at the top

# 28 Spring 2010 CS105 Case sensitivity MySQL is running on UNIX, therefore it is case sensitive However, inside a search string, so far we are finding that case doesn’t matter—both these work:

# 29 Spring 2010 CS105 Calculated field – Lesson 7 in Sams AS sets up an alias for a calculated value

# 30 Spring 2010 CS105 Another calculated field:

# 31 Spring 2010 CS105 Another Alias field …before:

# 32 Spring 2010 CS105 How do we make it look attractive? Instead of Detroit MI Why not Detroit, MI 44444

# 33 Spring 2010 CS105 Joining Words Together: Concatenation Concatenation is putting two words together Concatenation can be done with a function that takes two or more arguments separated by commas In SQL it works like this: Concat(field one,field two)

# 34 Spring 2010 CS105 However, it could look ugly concat(cust_city,cust_state,cust_zip) DetroitMI44444 How do we add a space? How do we insert a comma?

# 35 Spring 2010 CS105 After: SQLyog uses the CONCAT function – creates an Alias field by putting 3 columns together use Concat function rather than material shown in SAMs

# 36 Spring 2010 CS105 Commenting your SQL code You start comment lines with /* and end with */

# 37 Spring 2010 CS105 Remark or Comment Statement in SQL, Excel, VBA Used for documentation. Not “executable” SQLyog uses green font Most of the work you do out in the workplace is maintaining code--code without comments is unusable, and you have to start from scratch (!)

# 38 Spring 2010 CS105 To Summarize: How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field?