Structured Query Language SQL Unit 2 An Introduction to Organizing and Retrieving Data with SQL.

Slides:



Advertisements
Similar presentations
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Advertisements

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.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Chapter 2 Basic SQL SELECT Statements
Chapter 3 Single-Table Queries
Database Queries. Queries Queries are questions used to retrieve information from a database. Contain criteria to specify the records and fields to be.
Microsoft Access 2010 Building and Using Queries.
INLS 623– S QL Instructor: Jason Carter. SQL SELECT DISTINCT SELECT DISTINCT column_name, column_name FROM table_name ;
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
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
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Database Applications – Microsoft Access Lesson 4 Working with Queries 36 Slides in Presentation.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
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.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Structured Query Language SQL Unit 3 An Introduction to Organizing and Retrieving Data with SQL.
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
Structured Query Language SQL Unit 4 Solving Problems with SQL.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Session 1 Retrieving Data From a Single Table
SQL Query Getting to the data ……..
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Aggregating Data Using Group Functions
Building and Using Queries
(SQL) Aggregating Data Using Group Functions
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Database Queries.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
SQL – Entire Select.
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Access: SQL Participation Project
M1G Introduction to Database Development
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Shelly Cashman: Microsoft Access 2016
Aggregating Data Using Group Functions
Presentation transcript:

Structured Query Language SQL Unit 2 An Introduction to Organizing and Retrieving Data with SQL

Some More Interesting Single Table Queries In the last segment, we examined the history of SQL, a little bit about why we organize databases as we do and wrote our first SQL query with a single table We want to continue creating single table SQL queries so we can move to more interesting SQL statements Later we will design some really neat queries using SQL aggregate functions and multiple tables But now, let’s play around with some more single table stuff

The WHERE Argument Noting that our format for single table queries as: SELECT, (also “*”) FROM WHERE ORDER BY DESC We want to examine the WHERE argument, which is optional WHERE allows us to filter records by selecting values in a field Don’t forget the commas

The Logical Operators SQL uses the math operators to select records by field value OperatorMeaning > >= Greater than, Greater than or equal to < <= Less than, Less than or equal to <> Not equal to = Equal to And Include multiple logical items Or Test for individual logic items LIKE LIKE s% - to find all fields (columns) starting with “s”

An Example for WHERE Let’s try this example for WHERE: For a paper, you need to find all months were the Federal Funds Rate was less than zero; using the FedFundsRate table SELECT Period,Rate FROM FedFundsRate WHERE rate < 1.0 Click on the Execute icon when you have completed the entry

Here is the Recordset from WHERE

The IN Operator with WHERE The “IN” operator allows SQL to list all of the items a WHERE keyword will include in a filter SELECT division,department, category,item, year,Budget,actual,variance FROM Budgets WHERE item IN('training','telephone','Maintenance') Also, NOT IN gives results of all fields that are NOT in the list

IN Operator Results

Another Example for WHERE Let’s try another example for WHERE Assume you have a need to find out how many months had a Federal Funds Rate of 5.55 SELECT Period, Rate FROM FedFundsRate WHERE Rate = 5.55 Click on the Execute icon when you have completed the entry

Another Recordset Using WHERE

The ORDER BY Clause In many cases, it is necessary to ensure that our data is sorted Let’s look at a common SELECT statement that includes an ORDER BY clause SELECT, (also “*”) FROM WHERE ORDER BY DESC If the DESC (for Descending) is omitted, the Ascending sort is assumed DESC is for Descending

Wild Card Characters with LIKE The LIKE keyword is a powerful tool to select records with a particular pattern SQL uses “wild cards” to support this function Wild Card CharacterMeaning %A substitute for zero or more characters _ (underscore)A substitute for one character [Charcter List]Sets and ranges of characters to match [^ Character List] or [! Character List] Matches only a character NOT specified within the brackets

Finding Records with LIKE The LIKE operator is used to select records that has a field (or column) that matches a particular pattern SELECT division,department, category,item, year,Budget,actual,variance FROM Budgets WHERE Department LIKE 'd%‘ WHERE Departments LIKE ‘%d%’ The % sign means ANY OTHER character

Results of LIKE Query

Using the AND & OR Operators with WHERE It is too simplistic an approach to believe that your need to query databases would only have a single field (column) condition It is much more likely that your testing will include multiple conditions SQL includes the AND & OR operators to allow multiple logical conditions

Using the AND & OR Operators with WHERE Your manager wants to see all large departments (sales > 3000) with a negative budget variance SELECT division,department, category,item, year,Budget,actual,variance FROM Budgets WHERE Budget > 3000 AND Variance <0 AND Category LIKE '%F%'

And/OR Logical Actions

Sorting Using ORDER BY Your manager needs to have department budgets sorted by their variance from Budget vs Actual to see the worst first SELECT Division, department, Year, Budget, Actual, Variance FROM Budgets ORDER BY Variance Also modify the query to use the DESC sort

SELECT with ORDER BY Notice Vertical format

SELECT with ORDER BY DESC

SELECT with DISTINCT In a table, a column (field) may contain many duplicate values; and sometimes you only want to list the different (distinct) values. SELECT DISTINCT Division FROM Budgets ORDER BY Division

SELECT with DISTINCT Five (5) Divisions

Using an ALIAS An Alias is a method to override a field or column name with one of your own choice SELECT Division, department, Year, Budget, Actual, Variance AS Difference FROM Budgets ORDER BY Variance AS creates a different column Name

SQL BETWEEN Operator Allows the query to SELECT records or (rows) that are between a range of values SELECT Division, Department, Category, Item, Variance FROM Budgets WHERE Variance BETWEEN 2000 AND 3000

BETWEEN Example

Some Very Simple Functions SQL has many functions that assist data and financial analysts to be as finite as possible with data FunctionPurpose Count( )The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column Count(*)Counts the number of records in a Table Avg()The AVG() function returns the average value of a numeric column SUM()The SUM() function returns the total sum of a numeric column. MAX()The MAX() function returns the largest value of the selected column. ROUND(Column, Decimals) The ROUND() function is used to round a numeric field to the number of decimals specified. Now()Returns the current date and time

SQL Counting One of the functions of a SQL query is to count the records in a recordset or table SELECT COUNT(*) FROM Budgets SELECT COUNT(*) FROM MusicList SELECT COUNT(*) FROM AvePrimeRate

COUNT Results Using LIKE

Using Multiple Functions Your manager wants to know the sum of all budgets for the corporation SELECT sum(budget), avg(budget), max(budget),min(budget), stddev(budget) FROM Budgets

Multiple Functions with WHERE WHERE with logic

More Multiple Functions Your manager now wants you to calculate the values for only the Data Processing and Human Resources departments SELECT sum(budget), avg(budget), max(budget),min(budget),count(budget), stddev(Budget) ‘Population STDev FROM Budgets WHERE Department IN('human resources','data processing')

Multiple Functions with WHERE and IN IN function

Unit 2 Summary We have examined some query examples, staying away from one of the more powerful functions in SQL Single table queries are straightforward SQL queries using multiple tables are still straightforward, but just a bit more interesting We call these queries JOIN queries We will review these after a break