Advanced Select Statements Select List Variations SELECT *SELECT * Column NamingColumn Naming Arithmetic ExpressionsArithmetic Expressions ConstantsConstants.

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
Advertisements

Database Systems: Design, Implementation, and Management Tenth Edition
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Introduction to Structured Query Language (SQL)
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)
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
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.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
1 Section 5 - Grouping Data u The GROUP BY clause allows the grouping of data u Aggregate functions are most often used with the GROUP BY clause u GROUP.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
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
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Grouping Data. GROUP BY clause Groups results by column name used with aggregate functions must come first when used with ORDER BY.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
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.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
IFS180 Intro. to Data Management Chapter 11 - Subqueries.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
SQL Query Getting to the data ……..
Structured Query Language
Relational Database Design
Writing Basic SQL SELECT Statements
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
SQL – Where.
Writing Basic SQL SELECT Statements
IST 210: Organization of Data
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Chapter 4 Summary Query.
Chapter 7 Introduction to Structured Query Language (SQL)
Writing Basic SQL SELECT Statements
Section 4 - Sorting/Functions
Restricting and Sorting Data
Group Operations Part IV.
Presentation transcript:

Advanced Select Statements

Select List Variations SELECT *SELECT * Column NamingColumn Naming Arithmetic ExpressionsArithmetic Expressions ConstantsConstants StringsStrings

Column Naming Using column aliases we can control how the column names are displayed in our output. SELECT au_fname First_Name FROM authors The output will reflect this column name. First_Name JoeDanSue

Expressions Arithmetic expressions can be used in the select list of a SELECT statement. Column (+-*/) ColumnColumn (+-*/) Column eg. Unit_cost * items_soldeg. Unit_cost * items_sold Constant (+-*/) ColumnConstant (+-*/) Column eg. Unit_cost * 1.1eg. Unit_cost * 1.1 ConstantConstant FunctionsFunctions eg. Avg(sales)eg. Avg(sales)

Expressions allow us... To make projections To do “What if” analyses Alter reported data without altering the physical data

Precedence Hierarchy of Arithmetic Operators Parentheses Multiplication, Division Addition, Subtraction (2+2*3)/4 = 2

The Where Clause Arithmetic (+-*/) /Comparative (> <=,!=) Operators WHERE cost * 2 >= 20 WHERE state != ‘CA’ Logical Operators (AND, OR, NOT)Logical Operators (AND, OR, NOT) Ranges (BETWEEN, NOT BETWEEN)Ranges (BETWEEN, NOT BETWEEN) Lists (IN, NOT IN)Lists (IN, NOT IN) WHERE state NOT IN (‘CA’, ‘MD’, ‘NJ’) Unknowns (IS NULL, IS NOT NULL)Unknowns (IS NULL, IS NOT NULL) Character Matches (LIKE, NOT LIKE)Character Matches (LIKE, NOT LIKE) WHERE au_lname LIKE (‘Gr%’) OR au_lname LIKE (‘_reen’)

Precedence Hierarchy of Logical Operators Parentheses Multiplication, Division Addition, Subtraction NOT AND OR

Lists Can save a lot of typing! Can use a list or a sub query List example: Select au_lname, au_fname from authors where state in (‘CA’, ‘IN’, ‘MD’) SubQuery Example: Select pub_name from publishers where pub_id in (select pub_id from titles where total_sales > 3000)

Character Matches % - 0 or more character% - 0 or more character _ - 1 character_ lets you escape a - lets you escape a wildcard You can use 1 or more wildcards in a string.You can use 1 or more wildcards in a string. Eg. I%a = Indiana, Iowa, Ina-goda-da-vida

Things to Remember Logical operators can only be used with the WHERE clauseLogical operators can only be used with the WHERE clause The wildcards % and _ can only be used with LIKE and NOT LIKEThe wildcards % and _ can only be used with LIKE and NOT LIKE Wildcards can be used before and after the search string.Wildcards can be used before and after the search string. Strings must be quotedStrings must be quoted NULL is not the same as zeroNULL is not the same as zero

Aggregate Functions SUM ([DISTINCT] expr) The total sum of values in expression AVG ([DISTINCT] expr) The average of values in the expression COUNT ([DISTINCT] expr) The number of non-null values in the expression MAX (expr) The highest value in the expression MIN (expr) The lowest value in the expression

Syntax SELECT avg(price * 2) from titles returns the average of all prices * 2returns the average of all prices * 2 SELECT sum (price) from titles return the sum of the pricesreturn the sum of the prices