Download presentation
Presentation is loading. Please wait.
1
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
2
SELECT * FROM company Simple SELECT SQL Command Verb Columns to
Display SELECT * FROM company Table to Select Data from
3
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
4
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
5
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
6
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)
7
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 salesperson Result SELECT DISTINCT dept_no, …. FROM salesperson dept_no 1 2 3
8
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
9
Ch9 Practical1 - Basic Selects (single table)
Limiting columns Calculations using functions Aliases Sorting and removing duplicates
10
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
11
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
12
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
13
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
14
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
15
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
16
Ch9 Practical2 - Basic Selects (SingleTable)
Restricting rows using a ‘WHERE’ clause
17
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: 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
18
Ch9 Practical3 - Basic Selects (SingleTable)
Using NULLS and functions relating to NULLS
19
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.