Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.

Similar presentations


Presentation on theme: "Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming."— Presentation transcript:

1 Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming languages.  Use SQL SELECT statement effectively to retrieve the data from a particular table.  Complete Assignment 2 (Part II-c). Data Manipulation 1

2 2 Supplier-Part Database Table S Table P sno | sname | status | city pno | pname | color | weight | city ------------------------------ ---------------------------------- s1 | Smith | 20 | London p1 | nut | red | 12 | London s2 | Jones | 10 | Paris p2 | bolt | green | 17 | Paris s3 | Blake | 30 | Paris p3 | screw | blue | 17 | Rome s4 | Clark | 20 | London p4 | screw | red | 14 | London s5 | Adams | 30 | Athens p5 | cam | blue | 12 | Paris p6 | cog | red | 19 | London Table SP sno | pno | qty --------------- s1 | p1 | 300 s1 | p2 | 200 s1 | p3 | 400 s1 | p4 | 200 s1 | p5 | 100 s1 | p6 | 100 s2 | p1 | 300 s2 | p2 | 400 s3 | p2 | 200 s4 | p2 | 200 s4 | p4 | 300 s4 | p5 | 400

3 Data Manipulation 13 SQL DML Statements  Retrieval  SELECT * FROM s;  Update  UPDATE s SET status = 100;  Insert  INSERT INTO s VALUES ('s10', 'HP', 0, 'Corvallis');  Delete  DELETE from s WHERE city = 'London';

4 Data Manipulation 14 Data Manipulation by SQL  Declarative, non-procedural  Records to be retrieved are specified by a search condition to be satisfied.  Set-at-a-time access  One statement is applied to possibly multiple records.  Join by values  Records are related by attribute values, not by pointers.

5 Data Manipulation 15 SELECT Queries SELECT [DISTINCT] select-list FROM from-list WHERE qualification

6 Data Manipulation 16 What are the S#’s and statuses of the suppliers located in Paris? sno | status ------------ s2 | 10 s3 | 30 select sno, status from s where city = 'Paris'; Table S sno | sname | status | city ----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens

7 Data Manipulation 17 What are the S#’s and names of the suppliers whose statuses are greater than or equal to 20? sno | sname ------------ s1 | Smith s3 | Black s4 | Clark s5 | Adams select sno, sname from s where status >= 20; Table S sno | sname | status | city ----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens

8 Data Manipulation 18 Get the names and weights of the green parts. pno pname color weight city ---- ------ ------- ------ ------ p1 nut red 12 London p2 bolt green 15 Paris p3 screw green 17 Rome select pname, weight from p where color = 'green'; pname weight ----- ------ bolt 15 screw 17

9 Data Manipulation 19 Get the names and colors of the green parts. pno pname color weight city ---- ------ ------- ------ ------ p1 nut red 12 London p2 bolt green 15 Paris p3 screw green 17 Rome select pname, color from p where color = 'green'; pname color ----- ------ bolt green screw green

10 Data Manipulation 110 What are the distinct part numbers in the shipment records? table sp sno | pno | qty --------------- s1 | p1 | 300 s1 | p3 | 400 s1 | p5 | 100 s1 | p6 | 100 s2 | p1 | 300 s4 | p5 | 400 select distinct pno from sp; pno ---- p1 P3 P5 p6

11 Data Manipulation 111 What are the distinct cities of the suppliers? city -------- London Paris Athens select distinct city from s; Table S sno | sname | status | city ----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens

12 Data Manipulation 112 What are the S#'s and statuses of the suppliers located in Paris? Sort the resultant table in the order of the statuses. sno | status -------------- s2 | 10 s3 | 30 select sno, status from s where city = 'Paris' order by status ; Table S sno | sname | status | city ----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens

13 Data Manipulation 113 What are the S#'s, cities, and statuses of the suppliers? Sort the resultant table first in the ascending order of the cities and then in the desending order of statuses. sno | city | status ----------------------- s5 | Athens | 30 s1 | London | 20 s4 | London | 20 s3 | Paris | 30 s2 | Paris | 10 select sno, city, status from s order by city, status desc; Table S sno | sname | status | city ----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens

14 Data Manipulation 114 What is the weight in grams of each part? Order the resultant rows first by weights and then by P#'s. Table P pno | pname | color | weight | city ------------------------------------- p1 | nut | red | 12 | London p2 | bolt | green | 17 | Paris p3 | screw | blue | 17 | Rome p4 | screw | red | 14 | London p5 | cam | blue | 12 | Paris p6 | cog | red | 19 | London

15 Data Manipulation 115 What is the weight in grams of each part? Order the resultant rows first by weights and then by P#'s. pno | weight in grams = | grams ------------------------------ p1 | weight in grams = | 5448 p5 | weight in grams = | 5448... p6 | weight in grams = | 862 select pno, ' weight in grams = ', weight * 454 as grams from p order by 3 desc, pno ;

16 Data Manipulation 116 Get the S#'s, cities, and statuses of the suppliers in Athens or Paris. Sort the resultant table in the descending order of statuses. sno | city | status ----------------------- s3 | Paris | 30 s5 | Athens | 30 s2 | Paris | 10 select sno, city, status from s where city in ('Athens', 'Paris') order by status desc; Table S sno | sname | status | city ----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens

17 Data Manipulation 117 Get the S#'s, cities, and statuses of the suppliers in Athens or Paris. Sort the resultant table in the descending order of statuses. sno | city | status ----------------------- s3 | Paris | 30 s5 | Athens | 30 s2 | Paris | 10 select sno, city, status from s where city = 'Athens' or city = 'Paris' order by status desc; Table S sno | sname | status | city ----------------------------- s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens

18 Data Manipulation 118 select pname, weight from p where color = ' blue ' and weight < 20 order by weight asc; Get the names and weights of the blue parts that weigh less than 20 lbs. The parts should be listed according to their weights with lighter ones first. pname | weight ----------------- cam | 12 screw | 17


Download ppt "Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming."

Similar presentations


Ads by Google