Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

CSC271 Database Systems Lecture # 11.
Database Systems: Design, Implementation, and Management Tenth Edition
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
VIEWS Pertemuan 7 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
Sub Queries Pertemuan 5 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Overview Relational Databases and SQL Pertemuan 1 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Structured Query Language Part I Chapter Three CIS 218.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
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 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
IFS Intro. to Data Management Chapter 6 Filtering your data.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
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
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
 Agenda 2/20/13 o Review quiz, answer questions o Review database design exercises from 2/13 o Create relationships through “Lookup tables” o Discuss.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
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.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
SQL Query Getting to the data ……..
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Basic select statement
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
SQL FUNDAMENTALS CDSE Days 2018.
(SQL) Aggregating Data Using Group Functions
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL LANGUAGE and Relational Data Model TUTORIAL
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
Structured Query Language
CS4222 Principles of Database System
Section 4 - Sorting/Functions
Shelly Cashman: Microsoft Access 2016
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007

2 AGENDA: Retrieving Information from Tables Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions Working with Expressions Book: Mastering SQL by Martin Gruber Sybex (2000) Chapter : 7-9

3 Retrieving Information from Tables Using the SELECT Statement Retrieve the information in Salespeople table with SELECT statement: SELECT snum, sname, city, comm FROM Salespeople; SELECT : a keyword that tells the database this statement is a query. Snum, sname, city, comm : a list of the columns from the table that are being selected by the query. Columns that are not listed here, will not be included as the output. FROM Salespeople : a keyword (like SELECT), that must be present in every query, followed by a space and then the name of the table being used as the source of information (Salespeople).

4 Retrieving Information from Tables (cont’d) Selecting all columns SELECT * FROM Salespeople; Selecting only certain columns SELECT sname, comm FROM Salespeople; Eliminating Redundant Data – SELECT DISTINCT snum FROM Orders; – DISTINCT applies to all of the columns specified in the SELECT clause. DISTINCT vs. ALL – DISTINCT : eliminates redundant data from the entire output rows. – ALL : opposite of DISTINCT, it retained the duplicate data as the output rows.

5 Retrieving Information from Tables (cont’d) Qualified Selection – The WHERE Clause SQL enables you to define criteria to determine which rows are selected for output. Using WHERE clause. If the condition of the defined criteria at the WHERE clause is TRUE then the data will be retained for output, vise versa. Example: SELECT sname, city FROM Salespeople WHERE city = ‘London’;

6 Retrieving Information from Tables (cont’d) Using Inequalities in Predicates Equal to = Greater than > Less than < Greater than or equal to >= Less than or equal to <= Not equal to <> Example: SELECT * FROM Customers WHERE rating > 200;

7 Retrieving Information from Tables (cont’d) Using Boolean Operators in Predicates NOT SELECT * FROM Customers WHERE NOT city = ‘San Jose’; AND SELECT * FROM Customers WHERE city = ‘London’ AND rating < 200; OR SELECT * FROM Orders WHERE NOT ((odate = ’10/02/2000’ AND snum > 1002) OR amt > );

8 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions The IN Operator IN explicitly defines a set in which a given value may or may not be included. Example: – SELECT * FROM Salespeople WHERE city = ‘Barcelona’ OR city = ‘London’; – SELECT * FROM Salespeople WHERE city IN (‘Barcelona’, ’London’);

9 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) The BETWEEN Operator Similar to IN, but it is sensitive to order (first value must be first in alphabetic or numeric order), and followed by beginning value, the keyword AND, and the ending value. Example: – SELECT * FROM Salespeople WHERE comm BETWEEN.12 AND.10; – SELECT * FROM Salespeople WHERE comm >=.12 AND comm <=.10;

10 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) The LIKE Operator It is used with text datatypes To find substrings It searches a text column to see if part of it matches a string To support this, it uses wildcards, special characters that will match anything. Two type of wildcards: – Underscore characters (_) – Percent sign (%)

11 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Example: – SELECT * FROM Customers WHERE cname LIKE ‘G%’; – SELECT * FROM Salespeople WHERE sname LIKE ‘P_ _ L’; The IS NULL Operator Try to search records in Customers table where the value of city is NULL SELECT * FROM Customers WHERE city IS NULL;

12 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Using NOT with Special Operators SELECT * FROM Customers WHERE city IS NOT NULL; – It eliminates NULL values from the output. SELECT * FROM Customers WHERE NOT city IS NULL; – It produces output that contain no NULL values in city. SELECT * FROM Salespeople WHERE city NOT IN (‘London’, ‘San Jose’); – It is the same as  WHERE NOT city in (‘London’, ‘San Jose’); Can also be used in : NOT BETWEEN, NOT LIKE.

13 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Aggregate Functions Aggregate functions produce a single value for an entire group of columns. List of aggregate functions: – COUNT : produces the number of rows or non-NULL column values that the query selected. – SUM : produces the arithmetic sum of all selected values of a given column – AVG : produces the average (mean) of all selected values of a given column – MAX : produces the largest of all selected values of a given column – MIN : produces the smallest of all selected values of a given column

14 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Using Aggregate Functions: – Are used like column names in the SELECT clause of queries, and take column as arguments. Example: – SELECT SUM(amt) FROM Orders; – SELECT AVG(amt) FROM Orders; Special Attributes of COUNT: – SELECT COUNT(DISTINCT snum) FROM Orders; – SELECT COUNT(*) FROM Customers; Including Duplicates in Aggregate Functions – SELECT COUNT(ALL rating) FROM Customers; – Using ALL with COUNT  It will not count NULL values, and it takes a column name as an argument.

15 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Aggregate Built on Expression – SELECT AVG(comm * 100) FROM Salespeople; The GROUP Clause: – It allows you to define a subset of the values in a particular column and apply an aggregate function to the subset. – SELECT snum, odate, MAX(amt) FROM Orders GROUP BY snum, odate;

16 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) The HAVING Clause: For example, you want to do this: SELECT snum, odate, MAX(amt) FROM Orders WHERE MAX(amt) > GROUP BY snum, odate; You can not do the above, because you can not use aggregate functions in a WHERE clause (unless using Subquery), because predicates are evaluated in terms of a single row, whereas aggregate functions are evaluated in term of group of rows. Solution  use HAVING.

17 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) The correct query : SELECT snum, odate, MAX(amt) FROM Orders GROUP BY snum, odate HAVING MAX(amt) > ;

18 Working with Expressions Using Value Expression in the SELECT Clause To perform simple numeric computations on the data to put it in a form more appropriate to your needs. SQL allows you to place scalar expressions and constants among the selected columns. These expressions can replace columns in the SELECT clause. Naming expression columns in output by using AS. Example: SELECT snum, sname, city, comm * 100 AS percentage FROM Salespeople;

19 Working with Expressions (cont’d) Putting text in your query output – SELECT ‘For’, odate, ‘, there are’, COUNT(DISTINCT onum), ‘orders’ FROM Orders GROUP BY odate;

20 Working with Expressions (cont’d) More Advanced Uses of Values Expression Datetime and Interval Expressions – Several datetime datatypes: DATE TIME TIMESTAMP – INTERVALs represent periods of time between DATETIME values, for example. One week, one hour, or three days, ten minutes, and four seconds. – Example: INTERVAL 4 years, 11 months  INTERVAL YEAR TO MONTH ‘4/11’ DATE ‘2/4’98’ + INTERVAL YEAR TO MONTH ‘4/11’ DATE ‘2/4’98’ – DATE ‘2/2’98’ INTERVAL DAY ‘2’

21 Working with Expressions (cont’d) Datatype Conversion (CAST) Expressions – Convert one datatype to another within SQL – Does not affect the datatype of the data in the database – Only changes the datatype for the purposes of the current SQL statement – Primary reasons to change datatypes are: To make comparisons that would otherwise be invalid To control how a conversion that could be done automatically is actually performed To produce a datatype that can be properly handled in another language, for example in Embedded SQL, or in an interface. – Example  CAST (’12/03/2001’ AS DATE) – Example  SELECT CAST(onum AS CHAR), CAST(amt AS CHAR) FROM Orders;

22 Working with Expressions (cont’d) Conditional (CASE) Expressions – There are two types of CASE variations: CASE using value expressions CASE using predicates – Example: SELECT CASE sname WHEN ‘Peel’ THEN ‘Peal’ ELSE sname END FROM Salespeople WHERE snum = 1001; SELECT CASE WHEN sname = ‘Peel’ THEN ‘Peal’ ELSE sname END FROM Salespeople WHERE snum = 1001;

23 Working with Expressions (cont’d) Rules apply for any form of CASE: – Can not mix two forms – Either form can take any number of WHEN... THEN clause – An optional ELSE clause may follow all the WHEN... THEN clauses – Entire CASE form is terminated with the keyword END NULLIF : is a simple variation on CASE. It takes two values as arguments. If the two are the same, it produces a NULL; otherwise, it produces the first value of the two. COALESCE : it traverses a list of values and produces the first one that is not NULL. If all are NULL< it produces NULL.

24 Working with Expressions (cont’d) Ordering Output by Column Values Tables are unordered sets Use ORDER BY statement Can specify Ascending (ASC) or Descending (DESC)  Default is Ascending (ASC)

25 Working with Expressions (cont’d) Ordering by Multiple Columns – SELECT * FROM Orders ORDER BY cnum DESC, amt DESC; Ordering Aggregate Groups – SELECT snum, odate, MAX(amt) FROM Orders GROUP BY snum, odate ORDER BY snum; Ordering Output by Column Number – SELECT sname, comm FROM Salespeople ORDER BY 2 DESC

26 End of Retrieving Information Thank you