CS3220 Web and Internet Programming More SQL

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Information Resources Management February 27, 2001.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
SQL – Logical Operators and aggregation Chapter 3.2 V3.0 Napier University Dr Gordon Russell.
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
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
Lecture8:Data Manipulation in SQL Advanced SQL queries Ref. Chapter5 Lecture8 1.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
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)
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
SQL queries ordering and grouping and joins
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
Nikolay Kostov Telerik Corporation
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
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.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
Southern Methodist University CSE CSE 2337 Introduction to Data Management Chapter 2.
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.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.
More SQL: Complex Queries,
CS320 Web and Internet Programming SQL and MySQL
Writing Basic SQL SELECT Statements
Database Systems: Design, Implementation, and Management Tenth Edition
CS 440 Database Management Systems
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
CS122 Using Relational Databases and SQL
Writing Basic SQL SELECT Statements
CS 405G: Introduction to Database Systems
IST 210: Organization of Data
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
Restricting and Sorting Data
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language
CS4222 Principles of Database System
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
CSC 453 Database Systems Lecture
CS3220 Web and Internet Programming SQL and MySQL
Projecting output in MySql
CS3220 Web and Internet Programming SQL and MySQL
CS4540 Special Topics in Web Development LINQ to Objects
CS4540 Special Topics in Web Development SQL and MS SQL
CSC 453 Database Systems Lecture
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

CS3220 Web and Internet Programming More SQL Chengyu Sun California State University, Los Angeles

Employees DB employees projects project_members id first_name last_name address supervisor_id 1 John Doe Street #215 null 2 Jane Street #711 projects project_members id name leader_id 1 Firestone 2 Blue project_id employee_id 1 2

Examples: Single-Table Selection 1. List the last names of the employees whose ids are less than 10 Remove duplicates Show results in alphabetic order 2. Find the id of Jane Doe 3. Find the names of the employees who do not have a supervisor Concatenate first name and last name

SQL Literals Number: 10, 30.2 String: ‘CPU’, ‘John’’s Kitchen’ Date: ‘2007-06-01’ Time: ‘12:00:00’ Boolean: 1, 0, true, false NULL

SQL Operators Arithmetic Comparison Logical String Other +, -, *, /, % <, >, <=, >=,=,<> between Logical and, or, not String like Other is null in distinct order by

LIKE Simple pattern matching %: any zero or more characters _: any single character

Common Functions in Databases Numerical functions String functions Date and time functions NULL related functions Aggregation functions Most functions are DBMS specific.

Functions in MySQL https://dev.mysql.com/doc/refman/5.7/en/functions.html

Examples: Join 4. List the employees who work on the project with id=1 5. List the employees who work on the project Blue 6. Find the name of Jane Doe’s supervisor

Cross Join A.K.A. Cartesian Product The results are all possible combinations of the rows from Table 1 with the rows from Table 2 table1 table2 A B a1 b1 a2 b2 C D c1 d1 c2 d2 c3 d3 A B C D a1 b1 c1 d1 c2 d2 c3 d3 a2 b2

Equi-Join Cross join with additional conditions select … from T1, T2 where … … … cross join additional conditions

Inner Join a.k.a Join Combine two rows (one from each table) if they meet the join condition In other words, the results include the matching rows from the two tables

Inner Join Example table1 table2 table1 inner join table2 on A=C A B 1 10 2 12 C D 1 23 3 32 4 34 table1 inner join table2 on A=C A B C D 1 10 23

Examples: Outer Join 7. Find the employees who are not working on any project

Outer Joins Include the results of an Inner Join and the unmatched rows from one or both join tables

Left Outer Join a.k.a. Left Join table1 10 2 12 table1 left outer join table2 on A=C A B C D 1 10 23 2 12 null table2 C D 1 23 3 32 4 34

Right Outer Join a.k.a. Right Join table1 10 2 12 table1 right outer join table2 on A=C A B C D 1 10 23 null 3 32 4 34 table2 C D 1 23 3 32 4 34

Full Outer Join a.k.a. Full Join table1 10 2 12 table1 full outer join table2 on A=C A B C D 1 10 23 2 12 null 3 32 4 34 table2 C D 1 23 3 32 4 34

Example: Aggregation Functions 8. Find the number of employees whose last name is Doe

Aggregation Functions Operate on multiple rows and return a single result sum avg count max and min

Be Careful with NULL inventory max(price)?? min(price)?? avg(price)?? product_id upc quantity price 1 1020301 20 100 2 1342193 null 200 3 max(price)?? min(price)?? avg(price)?? count(upc)?? count(*)?? sum(quantity) ??

Example: Aggregation Queries 9. List the number of employees for each project Order the results by the number of employees 10. List the number of projects each employee works on

Understand GROUP BY … Without aggregation/GROUP BY select section_id, student_id from enrollment; section_id student_id 13 1 43 2 33 4 53 6

… Understand GROUP BY With aggregation/GROUP BY select section_id, count(student_id) from enrollment group by section_id; Grouping attribute Aggregation attribute section_id student_id 13 1 43 2 33 4 53 6 count=1 count=2 count=1 count=2

How GROUP BY Works Calculate the results without aggregation/GROUP BY Divide the result rows into groups that share the same value in the grouping attribute(s) Apply the aggregation function(s) to the aggregation attribute(s) for each group The result attributes must be either a group attribute or a aggregation attribute.

Even More SQL Subquery Set operations Top N query Transactions