INSS 6511 Lecture 5 SQL Continued. INSS 6512 SUBQUERIES. Queries inside query There are times when you need information from a table to answer query related.

Slides:



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

SQL - Subqueries and Schema Chapter 3.4 V3.0 Napier University Dr Gordon Russell.
Chapter 4: SQL  Basic Structure  Data Definition Language  Modification of the Database  Set Operations  Aggregate Functions  Null Values  Nested.
CSC443 Integrity Set operations List operations. Review Commands: Create Drop Alter Insert Select.
Database Systems: Design, Implementation, and Management Tenth Edition
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
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.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Introduction to SQL J.-S. Chou Assistant Professor.
©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.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
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.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
SQL Oracle PL/SQL. Select SELECT column1, column2,...columnN FROM table_name WHERE condition; SELECT column1, column2,...columnN FROM table_name WHERE.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SINGLE ROW FUNCTIONS 1. CHARACTER MANIPULATION Prof. Carmen Popescu Oracle Academy Lead Adjunct.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
7 7 SQL Data Modification 4Spread throughout chapter 7.
1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.
Ch 7 Introduction to SQL. Learning Objectives The basic commands and functions of SQL How to use SQL for data administration (to create tables, indexes,
SQL – Structured Query Language
Structured Query Language
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
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.
INTRODUCTION TO SQL Chapter SELECT * FROM teacher WHERE INSTR (subject_id, ‘&1’)= 4 AND LOWER (subject_id) LIKE ‘HST%’ ; When prompted for the.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
1 Session 6: Database Best Practice iNET Academy Open Source Web Development.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 DBS201: More on SQL Lecture 2. 2 Agenda Select command review How to create a table How to insert data into a table.
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.
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.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
CS 3630 Database Design and Implementation
SQL Query Getting to the data ……..
CS3220 Web and Internet Programming More SQL
Structured Query Language
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Advanced SQL Advanced SQL Week 8 Comparison Operators
The Database Exercises Fall, 2009.
CS 3630 Database Design and Implementation
Restricting and Sorting Data
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 4 Summary Query.
Introduction to SQL Holliday - COEN 178.
Lecture 3 Finishing SQL
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Contents Preface I Introduction Lesson Objectives I-2
Structured Query Language – The Basics
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

INSS 6511 Lecture 5 SQL Continued

INSS 6512 SUBQUERIES. Queries inside query There are times when you need information from a table to answer query related to the same table or another table

INSS 6513 Format: SQL>SELECT …. FROM WHERE colname1 condition(SELECT col2. (SELECT. ))

INSS 6514 List customers whose balance is greater than the average balance of all customers Logic: To answer this query we need to know average balance of all customers We will have to put this in a sub query

INSS 6515 List customers whose balance are greater than the average balance of all customers SQL> select * from customer where cust_balance > (Select avg(cust_balance) From customer);

INSS 6516 give the customer balance of customer whose order is 123 Logic: Before we can find balance of customer whose order is 123 we need to find the cust_numb first

INSS 6517 JOINING TABLES when information needed is in more than one table, we need to join tables; WHERE clause in the select SQL statement creates a join. Note some queries can also be answered using sub query

INSS 6518 Rules FOR joining WHERE attribute1 condition attribute2 Ex: where employee.ssn=student.ssn Value(s) from one table are matched value(s) from other tables all matching values are attached allows joining of tables based on common attribute domains without the WHERE clause it will produce a Cartesian product also

INSS 6519 Give the names of salesperson and their customers in maryland SQL>Select cust_name, Sales_name from Customer C, salesperson S where c.sales_numb= s. sales_numb’ And Upper(c.cust_st) =‘MD’; C & S are aliases for tables Customer and Salesperson respectively

INSS BUILT-IN-FUNCTIONS AVG(value) MAX(value) MIN(value) SUM(value) STDDEV(value) VARIANCE(value) COUNT(value) Etc…

INSS Nested functions Select max (avg(grades)) etc..is allowed

INSS String functions Several strings can be concatenated Use string1||string 2 || implies + select custname|| ‘,’ || cust_street as address From customer;

INSS Substrings Returns substrings Format: Substr(stringvalue,m,n) Where m is the starting value and n is the length of characters

INSS Assume orders have the format:” Abc1234 cdf2345 etc.. Get the first and last part of the order Select substr (order_numb, 1,3),substr (order_numb,4,4) From order;

INSS Product (p_code, P_price) table P_CODE P_PRICE QER/ Q2/P Q1/L QQ QW /QTY /QWE /QPD HB AA T 4.99 P_CODE P_PRICE WRE-Q PVC23DRT 5.87 SM SW WR3/TT rows selected.

INSS Select three characters of price SQL> select substr(p_price,1,3) from product; SUB SUB rows selected.

INSS Padding characters Rpad (string, length,’set’) Lpad (string,length,’set’) 1* select rpad (p_code,15,'.') as rightpad,p_price from product SQL> / RIGHTPAD P_PRICE QER/ Q2/P Q1/L QQ QW /QTY /QWE /QPD HB AA T RIGHTPAD P_PRICE WRE-Q PVC23DRT SM SW WR3/TT rows selected.

INSS Length of string format length (string) Returns length of the string

INSS SQL> select P_code, length(P_code) from product; P_CODE LENGTH(P_CODE) QER/ Q2/P Q1/L QQ QW /QTY /QWE /QPD HB AA T 8 P_CODE LENGTH(P_CODE) WRE-Q 8 PVC23DRT 8 SM SW WR3/TT rows selected.

INSS Trimming data LTrim, Rtrim..remove unwanted characters Format: RTRIM (string, ‘set’) Ltrim (string, ‘set’) Set is the collection of characters you want to trim

INSS SQL> select P_code, length(rtrim (P_code,' ')) from product ; P_CODE LENGTH(RTRIM(P_CODE,'')) QER/ Q2/P Q1/L QQ QW /QTY /QWE /QPD HB AA T 8 P_CODE LENGTH(RTRIM(P_CODE,'')) WRE-Q 8 PVC23DRT 8 SM SW WR3/TT rows selected.

INSS Remove. From price SQL> select ltrim(p_price,'.'), p_code from product; LTRIM(P_PRICE,'.') P_CODE QER/ Q2/P Q1/L QQ QW /QTY /QWE /QPD HB AA T LTRIM(P_PRICE,'.') P_CODE WRE-Q 5.87 PVC23DRT 6.99 SM SW WR3/TT3 16 rows selected.

INSS INSTR function Allows searching for a string of characters, gives the position of the string but does Not cut off anything Format: Instr(string, start,occurrence) Start is the start of the string Occurrence is the position of occurrence that you want to search

INSS Search for first “/” in p_code SQL> select p_code, instr(p_code,'/') from product ; ; P_CODE INSTR(P_CODE,'/') QER/ Q2/P Q1/L QQ QW /QTY /QWE /QPD HB AA T 0 P_CODE INSTR(P_CODE,'/') WRE-Q 0 PVC23DRT 0 SM SW WR3/TT rows selected.

INSS Separate P_code in two parts: before _ and after _ for names that contain - SQL> select p_code, substr(P_code, 1, instr(p_code,'/')) part1 from product where p_code like '%/%'; 2 3 P_CODE PART QER/31 11QER/ 13-Q2/P2 13-Q2/ 14-Q1/L3 14-Q1/ 2232/QTY 2232/ 2232/QWE 2232/ 2238/QPD 2238/ WR3/TT3 WR3/ 7 rows selected.

INSS SQL> select p_code, substr(P_code, 1, instr(p_code,'/')-1) part1 from product where p_code like '%/%'; 2 3 P_CODE PART QER/31 11QER 13-Q2/P2 13-Q2 14-Q1/L3 14-Q1 2232/QTY /QWE /QPD 2238 WR3/TT3 WR3 7 rows selected.

INSS Get the right part SQL> select p_code, substr(P_code, instr(p_code,'/')) part2 from product where p_code like '%/%'; 2 3 P_CODE PART QER/31 /31 13-Q2/P2 /P2 14-Q1/L3 /L3 2232/QTY /QTY 2232/QWE /QWE 2238/QPD /QPD WR3/TT3 /TT3 7 rows selected.

INSS SQL> select p_code, substr(P_code, instr(p_code,'/')+1) part2 from product where p_code like '%/%'; 2 3 P_CODE PART QER/ Q2/P2 P2 14-Q1/L3 L3 2232/QTY QTY 2232/QWE QWE 2238/QPD QPD WR3/TT3 TT3 7 rows selected.

INSS SQL> select p_code, substr(P_code, 1, instr(p_code,'/')-1) part1, substr(P_code, instr(p_code,'/')+1) part2 from product where p_code like '%/%'; 2 3 P_CODE PART1 PART QER/31 11QER Q2/P2 13-Q2 P2 14-Q1/L3 14-Q1 L3 2232/QTY 2232 QTY 2232/QWE 2232 QWE 2238/QPD 2238 QPD WR3/TT3 WR3 TT3 7 rows selected.

INSS Remove the period from price

INSS SQL> select (substr(P_price, 1, instr(p_price,'.')-1) || substr (P_price, instr(p_price,'.')+1)) as price from product; 2 3 PRICE PRICE rows selected.

INSS COMMON ERRORS

INSS INTEGRITY CONSTRAINT VIOLATION SQL> select * from trial1; SN SCITY baltimore SQL> insert into trial2 values (234,222); insert into trial2 values (234,222) * ERROR at line 1: ORA-02291: integrity constraint (AGGARWAL.SYS_C ) violated - parent key not found

INSS TOO MANY VALUES SQL> l 1 select * from invoice where cust_code in 2* (select inv-num, inv_date from invoice) SQL> / (select inv-num, inv_date from invoice) * ERROR at line 2: ORA-00913: too many values

INSS LEASE TABLE SQL> desc lease; Name Null? Type L_NO NOT NULL CHAR(6) P_NO CHAR(5) RENTER_NO CHAR(4) RENT NUMBER(5) PAYMENT CHAR(5) START_DATE DATE FIN_DATE DATE

INSS MISMATCH COMPARISON SQL> / select * from lease where rent in (select payment from lease) * ERROR at line 1: ORA-01722: invalid number

INSS UNIQUE CONSTRAINT VIOLATION SQL> select * from trial1; SN SCITY baltimore SQL> insert into trial1 values (111,'mass'); insert into trial1 values * ERROR at line 1: ORA-00001: unique constraint (AGGARWAL.SYS_C ) violated

INSS NOT a Single_Group Function select distinct l_no, sum(rent) * ERROR at line 1: ORA-00937: not a single-group group function