SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1.

Slides:



Advertisements
Similar presentations
Database Systems: Design, Implementation, and Management Tenth Edition
Advertisements

Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
1 SQL-Structured Query Language SQL is the most common language used for creating and querying relational databases. Many users can access a database applications.
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.
Introduction to SQL J.-S. Chou Assistant Professor.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
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.
Data Manipulation Using MySQL tMyn1 Data Manipulation Using MySQL Ideally, a database language should allow a user to: –Create the database and relation.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
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,
Using Special Operators (LIKE and IN)
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.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
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.
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.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SQL: Join Training Problems D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Join Training Problems 1.
SQL: Sets UNION INTERSECT MINUS D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Sets 1.
SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 SQL II CIS*2450 Advanced Programming Concepts. 2 Data Types INTEGER –numbers without a decimal point –range is to SMALLINT –like.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Database Design lecture 3_2 Slide 1 Database Design Lecture 3_2 Data Manipulation in SQL Simple SQL queries References: Text Chapter 8 Oracle SQL Manual.
SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1.
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.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
SQL Query Getting to the data ……..
Structured Query Language
Chapter 5 Introduction to SQL.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Writing Basic SQL SELECT Statements
Basic select statement
SQL Implementation & Administration
Working with Tables: Join, Functions and Grouping
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 – Entire Select.
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Access: SQL Participation Project
Chapter 7 Introduction to Structured Query Language (SQL)
Section 4 - Sorting/Functions
MySQL SQL for MySQL (I) Salim Mail :
Presentation transcript:

SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1

SELECT: syntax SELECT [ DISTINCT | ALL ]{ * | column_list } FROM [owner.]table_name [alias] [,[owner.]table_name [alias]]... [WHERE condition] [GROUP BY column_list] [HAVING condition] [ORDER BY column_list]; Can accomplish the three relational operations: selection, projection, and join. The order of the clauses in the SELECT statement cannot be changed. The only required clauses are SELECT and FROM. D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 2

SELECT [ DISTINCT | ALL ]{ * | column_list } SELECTcarries out the relational operation of projection: select certain columns. column_listone/more column_names from the tables/views listed in the FROM clause. *All columns. The order of fields is the same as the order given in CREATE TABLE. In some dialects it is possible to place table_name.* ALLallows duplicated rows to appear in the result DISTINCTdoes not allow appearing of duplicate rows D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 3

SELECT [ DISTINCT | ALL ]{ * | column_list } Expressions affect every row of the result table literal constants: SELECT Item_id, descript, ‘available: ‘, on_hand FROM stock; arithmetic: +, -, *, / SELECT Item_id, ‘Value: ’, price*on_hand FROM stock; Use qualifier: first - to be sure about the result and second it is easier to read SELECT Item_id, price*on_hand AS ‘Value’ FROM stock; non aggregate functions: sqrt, sin, cos, etc D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 4

SELECT [ DISTINCT | ALL ]{ * | column_list } Aggregate functions: SUM, AVG, COUNT, MAX, MIN takes entire column(s) and produce a single value; predefined, affected by GROUP BY DISTINCTaffects SUM, AVG, MAX, MIN; not allowed in COUNT(*), but COUNT(DISTINCT column_name) EXAMPLE: SELECT DISTINCT cust_id, item_id FROM orders; EXAMPLE: SELECT COUNT(DISTINCT cust_id) FROM orders; D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 5

The FROM clause FROM Must immediately follow the required SELECT clause. Lists one or more tables and/or views. Defines the required join on those tables. D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 6

fancy_fruits DB (MySQL Implementation) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 7

Illustrative examples (1) cust_idcust_nameregionphone AAAALLICENE(555) BBBBILLW(555) CCCCAITLINNE(555) DDDCOLINS(555) EEEELIZABETHW(555) LLLLAURANE(555) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 8 fancy_fruits.customers SELECT region, cust_name FROM customers; SELECT cust_name, phone FROM customers;

Illustrative examples (2) order_nocust_iditem_idquantity 001AAAI BBBI AAAI CCCI BBBI AAAI AAAI EEEI CCCI BBBI0560 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 9 SELECT item_id, quantity FROM orders; SELECT cust_id, item_id, quantity FROM orders; fancy_fruits.orders

The WHERE clause WHEREControls, which rows are retrieved into the result: serve as filter and defines the search conditions simple comparisons =, <>, >, =, <= compound condition AND, OR, NOT special SQL predicate BETWEEN, IN, LIKE, NULL comparison with the result of a sub-query additional predicates for use with a sub-query: ANY, ALL, EXISTS D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 10

Illustrative examples (3) cust_idcust_nameregionphone AAAALLICENE(555) BBBBILLW(555) CCCCAITLINNE(555) DDDCOLINS(555) EEEELIZABETHW(555) LLLLAURANE(555) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 11 fancy_fruits.customers SELECT * FROM customers WHERE region = ‘NE’; SELECT * FROM customers WHERE region = ‘ne’;

Illustrative examples (4) cust_idcust_nameregionphone AAAALLICENE(555) BBBBILLW(555) CCCCAITLINNE(555) DDDCOLINS(555) EEEELIZABETHW(555) LLLLAURANE(555) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 12 fancy_fruits.customers SELECT cust_name FROM customers WHERE length(cust_name) > 4;

Illustrative examples (5) cust_idcust_nameregionphone AAAALLICENE(555) BBBBILLW(555) CCCCAITLINNE(555) DDDCOLINS(555) EEEELIZABETHW(555) LLLLAURANE(555) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 13 fancy_fruits.customers SELECT cust_name, phone FROM customers WHERE cust_name=‘CAITLIN’ OR cust_name=‘LAURA’;

Illustrative examples (6) cust_idcust_nameregionphone AAAALLICENE(555) BBBBILLW(555) CCCCAITLINNE(555) DDDCOLINS(555) EEEELIZABETHW(555) LLLLAURANE(555) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 14 fancy_fruits.customers SELECT * FROM customers WHERE phone IS NULL; phone = NULL | <> NULL

Illustrative examples (7) cust_idcust_nameregionphone AAAALLICENE(555) BBBBILLW(555) CCCCAITLINNE(555) DDDCOLINS(555) EEEELIZABETHW(555) LLLLAURANE(555) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 15 fancy_fruits.customers SELECT * FROM customers WHERE cust_name LIKE ‘Alice’; ‘%li%’ ‘%’ is a wildcard – zero or more characters ‘A_ _ c%’‘_’ one single character

Illustrative examples (8) order_nocust_iditem_idquantity 001AAAI BBBI AAAI CCCI BBBI AAAI AAAI EEEI CCCI BBBI0560 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 16 SELECT item_id, quantity FROM orders WHERE quantity > AVG(quantity) ; fancy_fruits.orders SELECT item_id, quantity FROM orders WHERE quantity > 50 ;

Illustrative examples (9) cust_idcust_nameregionphone AAAALLICENE(555) BBBBILLW(555) CCCCAITLINNE(555) DDDCOLINS(555) EEEELIZABETHW(555) LLLLAURANE(555) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 17 fancy_fruits.customers SELECT * FROM orders WHERE cust_id IN ( ‘CCC’, ‘EEE’, ’LLL’); a IN (x, y, z) is equivalent of (a = x) OR (a = y) OR (a = z)

Illustrative examples (10) cust_idcust_nameregionphone AAAALLICENE(555) BBBBILLW(555) CCCCAITLINNE(555) DDDCOLINS(555) EEEELIZABETHW(555) LLLLAURANE(555) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 18 fancy_fruits.customers SELECT * FROM orders WHERE quantity BETWEEN 20 AND 40; a BETWEEN x AND y is equivalent of (a >= x) AND (a <= y)

The ORDER BY clause Order according to a list of column_names from the list of column_names specified in SELECT. Column_numbers (according to the order of fields as they appeared in the SELECT clause) could be used also (useful when specified an expression without identifying it with AS clause). ASC (ascending) is the default option; DESC (descending) must be specified. When more than one order keys are used: the first one specifies the general order (major key), the second one (minor key) - the order into a group with the equal values of the major key, etc. D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 19

Illustrative examples (11) cust_idcust_nameregionphone AAAALLICENE(555) BBBBILLW(555) CCCCAITLINNE(555) DDDCOLINS(555) EEEELIZABETHW(555) LLLLAURANE(555) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 20 fancy_fruits.customers SELECT * FROM customers ORDER BY region, cust_name; SELECT * FROM customers ORDER BY 3, 1 DESC;

quack_consulting DB (MySQL Implementation) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 21

Quack Consulting DB Examples (1) client_idclient_namecityregionphone A001AliceBolcoS B002BillTranforW C003CaitlinCalpasNE D004ColinTranforW L005LauraCalpasNE D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 22 quack_consulting.Clients 1.List all clients from region W 2.List all clients, which names started with ‘C’ 3.What the following query will print? SELECT name, phone FROM clients WHERE phone LIKE ‘_ _ 4%’;

Quack Consulting DB Examples (2) skill_idskill_descrbilling_rate ANAnalysis60.00 DEDesign70.00 PRProgram50.00 CPConfig70.00 DDDatabase80.00 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 23 quack_consulting.Specialty 1.Show all specialties with current billing rate and the billing rate increased with 10%. 2.Find half the sum of the minimum billing rate plus the maximum billing rate. 3.Find all specialties with current billing rates that are lower than the average billing rate.

Quack Consulting DB Examples (3) project_idconsultantitemcost P01URIConnectors P01URIBoards P01SUECalculator20.00 P03RAYKeyboard P05TEDCharts5.00 P02TEDModem D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 24 quack_consulting.Purchases 1.Find the total cost of all purchases, associated with project P01. 2.Find the minimum, maximum, and average cost.

Quack Consulting DB Examples (4) project_idconsultantdatehours P01SUE P01SUE P03TED P01URI P03TED P05TED P03RAY P05TED D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 25 quack_consulting.Time 2.List all time entries for 4 or more hours logged by Sue or Ted between 6/25/2013 and 7/14/ List all time entries except those for which the consultant is Sue or the date falls during July Find all times logged for projects P01 and P03 by Ted and Sue.

Questions? Attention ! Next class Quiz 4: Single Table Queries D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 26