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