Basic Data Manipulation - Reading Data

Slides:



Advertisements
Similar presentations
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Advertisements

Introduction to Structured Query Language (SQL)
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.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
Introduction to Structured Query Language (SQL)
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Chapter 2 Basic SQL SELECT Statements
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
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.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Structured Query Language SQL Unit 2 An Introduction to Organizing and Retrieving Data with SQL.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 Chapter 2 Basic SQL SELECT Statements. 2 Chapter Objectives Distinguish between an RDBMS and an ORDBMS Identify keywords, mandatory clauses, and optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
 CONACT UC:  Magnific training   
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Relational Database Design
Writing Basic SQL SELECT Statements
Aggregating Data Using Group Functions
Basic select statement
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
02 | Advanced SELECT Statements
Using the Set Operators
Writing Basic SQL SELECT Statements
(SQL) Aggregating Data Using Group Functions
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Chapter 7 Introduction to Structured Query Language (SQL)
SQL DATA CONSTRAINTS.
Writing Basic SQL SELECT Statements
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Restricting and Sorting Data
Reporting Aggregated Data Using the Group Functions
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
Shelly Cashman: Microsoft Access 2016
Restricting and Sorting Data
Aggregating Data Using Group Functions
Presentation transcript:

Basic Data Manipulation - Reading Data Objectives To learn how to carry out simple manipulation on the data being read Contents The SELECT Statement The SELECT and FROM clauses Limiting columns, calculated columns, built-in functions, aliases The DISTINCT keyword Sorting the result set - the ORDER BY clause Practical 3-1 Row Selection options - the WHERE clause Practical 3-2 Nulls, nullability, three way logic Practical 3-3

SELECT * FROM company Simple SELECT SQL Command Verb Columns to Display SELECT * FROM company Table to Select Data from

SELECT * -- all columns FROM salesperson Statement Format SQL is a free format language, but syntactically extremely fussy use new lines, tab keys and indentation to make it readable white space is ignored by the parser Make use of comments SELECT * -- all columns FROM salesperson

SELECT company_no, name, county FROM company Specifying Columns You have 2 choices an ‘*’, or else list the columns comma-separated if there are 40 columns and you want only 39 of them, then list them columns may be listed in any order Columns to Display SELECT company_no, name, county FROM company

Calculated (Virtual) Columns and Aliases Some RDBMS support this 2nd notation for aliases Real Column Calculated (virtual) Column SELECT lname, sales_target * 1.2 AS ‘next year’ FROM salesperson Column Alias SELECT lname, ‘next year’ = sales_target * 1.2 FROM salesperson

Standard and non-Standard Functions SQL2 provides a standard set of scalar functions implementations vary - different names and arguments most engines offer further, non-standard functions A SQL standard function: SELECT order_no, Substring (their_order_no,1,3 ) FROM sale Non-standard examples SELECT order_no, Datepart(qq, order_date ) qtr_sold FROM sale -- (MS SQL Server) SELECT order_no, TO_CHAR(order_date,’Q’ ) qtr_sold FROM sale -- (Oracle)

Keyword - Distinct emp_no dept_no sales_target 10 1 23000 20 3 34500 ‘DISTINCT’ (if used) always follows SELECT emp_no dept_no sales_target 10 1 23000 20 3 34500 30 2 12000 40 3 36900 50 1 12780 60 3 12650 salesperson Result SELECT DISTINCT dept_no, …. FROM salesperson dept_no 1 2 3

Sorting the Results By Specific Column(s) SELECT * FROM contact NOTE - an ‘alias’ (defined in the SELECT clause) can only be used elsewhere in ‘ORDER BY’ Each part of the sort sequence is ‘ASC’ending unless DESC specified SELECT * FROM contact ORDER BY company_no DESC, name By Alias Name … ORDER BY month

Ch9 Practical1 - Basic Selects (single table) Limiting columns Calculations using functions Aliases Sorting and removing duplicates

Limiting rows with basic operators Think of a WHERE clause as an ‘IF’ statement A Basic Operator SELECT * FROM sale WHERE order_value > 2000 Value Limits rows to those matching the condition Numeric column

BETWEEN SELECT * FROM salesperson Saves typing “col >= x AND col <= y” but means the same thing. SELECT * FROM salesperson WHERE sales_target BETWEEN 100 AND 500 Starting Value Stopping Value

IN Peter George Tom Mike Sandy Eleanor Bill Gary Grace Harry Samantha Dick SELECT fname FROM salesperson WHERE fname IN (‘Fred’, ‘Tom’, ‘Dick’, ‘Harry’) -- easier than coding WHERE fname = ‘Fred’ OR fname = ‘Tom’ OR fname = ‘Dick’ OR fname = ‘Harry’ Tom Dick Harry salesperson output

NOT Peter George Tom Mike Sandy Eleanor Bill Gary Grace Harry Samantha Dick SELECT fname FROM salesperson WHERE fname NOT IN (‘Fred’, ‘Tom’, ‘Dick’, ‘Harry’) Peter George Mike Sandy Eleanor Gary Grace Samantha salesperson output

LIKE WHERE lname LIKE ‘A%’ - (starts with ‘A’) ….. lname LIKE ‘%A’ - (ends with ‘A’) ….. lname LIKE ‘%A%’ - (contains an ‘A’) WHERE lname LIKE ‘_T%N%R_’ means - has a ‘T’ in position 2 and has an ‘R’ in the penultimate position and has an ‘N’ somewhere in between eg ‘stationary’ would match ‘_’ Matches any single character ‘%’ Matches any number of characters (incl 0!) Note – LIKE may only be used with character columns

Multiple Conditions SELECT * FROM company WHERE county = ‘Surrey’ When mixing AND’s and OR’s use parentheses to clarify meaning SELECT * FROM company WHERE county = ‘Surrey’ AND town = ‘Harlow’ First condition Second condition Connector CONNECTOR TYPE PRECEDENCE AND Evaluated first OR Evaluated second

Ch9 Practical2 - Basic Selects (SingleTable) Restricting rows using a ‘WHERE’ clause

Nulls SELECT * FROM salesperson WHERE notes IS NULL Basic premise of an RDBMS is the concept of optional columns NULL means ’not applicable’ or ’unknown’ NULL is different from zero or blank On INSERT of a new row, must supply values for mandatory columns other columns will be NULL (assuming no ‘DEFAULT’ value) NULL propagates through expressions: 5 + null = null, not 5 An expression in a WHERE clause may evaluate to TRUE, FALSE or NULL only rows whose expressions evaluate to TRUE are output Use IS NULL to retrieve rows with NULL entries: SELECT * FROM salesperson WHERE notes IS NULL

Ch9 Practical3 - Basic Selects (SingleTable) Using NULLS and functions relating to NULLS

SUMMARY SELECT retrieves data The SELECT ‘columnlist’ says which columns FROM says which table, WHERE says which rows ORDER BY sorts, DISTINCT ensures no two rows of the output are the same NULLS are an unfortunate necessity and will come up many times You can type the word SELECT without thinking But you can’t type column names after it without having worked out the FROM clause! Remember this when you try to READ SQL later!! The logical sequence is actually FROM this table From the rows WHERE this is true Show (SELECT) me these columns or calculations