Basic SQL II. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Similar documents
Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

A taxonomy of SQL queries Learning Plan

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

CS 338 Basic SQL Part II

Basic SQL. Basic SQL. Basic SQL

SQL STRUCTURED QUERY LANGUAGE

Database design process

Database Technology. Topic 3: SQL. Olaf Hartig.

ECE 650 Systems Programming & Engineering. Spring 2018

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Slides by: Ms. Shree Jaswal

CSIE30600 Database Systems Basic SQL 2. Outline

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Overview Relational data model

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

Data Manipulation Language (DML)

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

L130 - DATABASE MANAGEMENT SYSTEMS LAB CYCLE-1 1) Create a table STUDENT with appropriate data types and perform the following queries.

Part 1 on Table Function

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Announcement5 SQL5. Create%and%drop%table5. Basic%SFW%query5. Reading%a%table5. TDDD37%% Database%technology% SQL5

DBMS LAB SESSION PAVANKUMAR MP

The Relational Algebra and Calculus. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Transaction Processing Dr Fawaz Alarfaj Al Imam Mohammed Ibn Saud Islamic University

SQL: Advanced Queries, Assertions, Triggers, and Views. Copyright 2012 Ramez Elmasri and Shamkant B. Navathe

Advanced Databases. Winter Term 2012/13. Prof. Dr. Dietmar Seipel University of Würzburg. Advanced Databases Winter Term 2012/13

Simple SQL Queries (contd.)

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

CIS 611. Copyright 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6-1

Chapter 8: Relational Algebra

Session Active Databases (2+3 of 3)

CIS611 Lab Assignment 1 SS Chung

Advanced Databases (SE487) Prince Sultan University College of Computer and Information Sciences

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Data Definition Language (DDL)

COSC Assignment 2

CSC 742 Database Management Systems

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

ECE 650 Systems Programming & Engineering. Spring 2018

Insertions, Deletions, and Updates

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Topics. CSCI 403 Database Management DISTINCT. JOIN Clause 2/4/2019 DISTINCT JOINS. 12 Miscellaneous Topics

Chapter 8: The Relational Algebra and The Relational Calculus

Relational Algebra 1

SQL Introduction. CS 377: Database Systems

Structured Query Language (SQL)

Integrity Coded Relational Databases (ICRDB) - Protecting Data Integrity in Clouds

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe Slide 25-1

Chapter 6 Part I The Relational Algebra and Calculus

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

DATABASE CONCEPTS. Dr. Awad Khalil Computer Science & Engineering Department AUC

NOTE: DO NOT REMOVE THIS EXAM PAPER FROM THE EXAM VENUE

Lecture 6 - More SQL

Structure Query Language (SQL)

Practical Project Report

CSEN 501 CSEN501 - Databases I

CS 377 Database Systems

CSC Web Programming. Introduction to SQL

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

More SQL: Complex Queries, Triggers, Views, and Schema Modification

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

RELATIONAL DATA MODEL

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Chapter 18 Strategies for Query Processing. We focus this discussion w.r.t RDBMS, however, they are applicable to OODBS.

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Querying a Relational Database COMPANY database For Lab4, you use the Company database that you built in Lab2 and used for Lab3

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

Chapter 6 The Relational Algebra and Calculus

Chapter 3. Algorithms for Query Processing and Optimization

CS 582 Database Management Systems II

CS5300 Database Systems

RELATIONAL DATA MODEL: Relational Algebra

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

UNIT 2 RELATIONAL MODEL

Relational Model. CS 377: Database Systems

CS 348 Introduction to Database Management Assignment 2

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Guides for Installing MS SQL Server and Creating Your First Database. Please see more guidelines on installing procedure on the class webpage

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Course Notes on Relational Algebra

Hand in: the database picture (png or jpeg or ) for question 2, the queries (as SQL statements) for question 4

Chapter 6 - Part II The Relational Algebra and Calculus

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

2.2.2.Relational Database concept

Relational Algebra. Relational Algebra Overview. Relational Algebra Overview. Unary Relational Operations 8/19/2014. Relational Algebra Overview

Database Technology. Topic 3: SQL. Structured Query Language. Creating Tables SQL DDL. Creating Tables (Example) Modifying Table Definitions

Transcription:

Basic SQL II Dr Fawaz Alarfaj Al Imam Mohammed Ibn Saud Islamic University ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Lab 1 Review Retrieve the birthdate and address of the employee(s) whose last name is king 2

Lab 1 Review SELECT bdate, address FROM employee WHERE lname = 'King' 3

Lab 1 Review Retrieve the first and last name of the employee(s) whose middle initial is A 4

Lab 1 Review SELECT fname, lname FROM employee WHERE minit = A 5

Lab 1 Review Retrieve departments names and first and last names of department's managers 6

Lab 1 Review SELECT dname, fname, lname FROM employee, department WHERE ssn = mgrssn 7

Lab 1 Review Retrieve first and last names of all married employees 8

Lab 1 Review SELECT fname, lname FROM employee, dependent WHERE essn = ssn AND relationship= Spouse' 9

Lab 1 Review Retrieve all departments (name and number) located in Houston 10

Lab 1 Review SELECT dname, department.dnumber FROM department, dept_locations WHERE department.dnumber = dept_locations.dnumber AND dlocation= Houston' 11

Lab 1 Review Retrieve the employee's first and last name for each employee whose immediate supervisor last name is king 12

Lab 1 Review SELECT e2.fname, e2.lname FROM WHERE AND employee as e1, employee as e2 e1.ssn = e2.superssn e1.lname='king' 13

Lab 1 Review List the first name, last name, and ID for each supervisor without duplication 14

Lab 1 Review SELECT fname, lname, ssn FROM WHERE employee, ssn in (SELECT DISTINCT superssn FROM employee) 15

Tables as Sets in SQL Set operations UNION, EXCEPT (difference), INTERSECT Corresponding multiset operations: UNION ALL, EXCEPT ALL, INTERSECT ALL) Type compatibility is needed for these operations to be valid 16

Tables as Sets in SQL 17

Tables as Sets in SQL Example Database ID Name Major 1123 Ali IS 1122 Naif CS 1124 Saad CS 1128 Omar CS Network ID Name Major 1124 Saad CS 1125 Hasan IS 1126 Hamad IS 1122 Naif CS 1127 Majed IS 18

Tables as Sets in SQL UNION operator returns all the unique rows from both the queries Select Id, Name, Major from Database UNION Select Id, Name, Major from Network 19

Tables as Sets in SQL ID Name Major 1124 Saad CS 1125 Hasan IS 1126 Hamad IS 1122 Naif CS 1127 Majed IS 1123 Ali IS 1128 Omar CS 20

Tables as Sets in SQL UNION ALL operator returns all the rows from both the queries, including the duplicates Select Id, Name, Major from Database UNION ALL Select Id, Name, Major from Network 21

Tables as Sets in SQL ID Name Major 1124 Saad CS 1125 Hasan IS 1126 Hamad IS 1122 Naif CS 1127 Majed IS 1123 Ali IS 1122 Naif CS 1124 Saad CS 1128 Omar CS 22

Tables as Sets in SQL INTERSECT operator retrieves the common unique rows from both the left and the right query. Notice the duplicates are removed. Select Id, Name, Major from Database INTERSECT Select Id, Name, Major from Network 23

Tables as Sets in SQL ID Name Major 1122 Naif CS 1124 Saad CS 24

Tables as Sets in SQL EXCEPT operator returns unique rows from the left query that aren t in the right query s results. Select Id, Name, Major from Database EXCEPT Select Id, Name, Major from Network 25

Tables as Sets in SQL ID Name Major 1123 Ali IS 1128 Omar CS 26

Substring Pattern Matching LIKE comparison operator Used for string pattern matching (%) replaces an arbitrary number of zero or more characters underscore (_) replaces a single character Examples: WHERE NAME LIKE H% ; WHERE NAME LIKE _ a ; 27

Substring Pattern Matching Quiz 28

Arithmetic Operators BETWEEN comparison operator SELECT * FROM employee WHERE(Salary BETWEEN 30000 AND 40000 ) ; 29

Arithmetic Operations Standard arithmetic operators: Addition (+), subtraction ( ), multiplication (*), and division (/) may be included as a part of SELECT 30

Arithmetic Operations Show the resulting salaries if every department s manager is given a 10 percent raise. SELECT Fname, Lname, 1.1 * Salary AS Increased_sal FROM WHERE EMPLOYEE, DEPARTMENT Ssn=mgrssn 31

Ordering of Query Results Use ORDER BY clause Keyword DESC to see result in a descending order Keyword ASC to specify ascending order Typically placed at the end of the query ORDER BY D.Dname DESC, E.Lname ASC, E.Fname ASC 32

Ordering of Query Results 33