0% found this document useful (0 votes)
269 views4 pages

Supplier and Parts Database Queries

The document outlines the structure and creation of three database tables: suppliers, parts, and catalog, along with their relationships. It includes SQL commands for inserting data into these tables and provides a series of queries to retrieve specific information about suppliers and parts. The queries cover various aspects such as supplier-part relationships, pricing, and color categorization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
269 views4 pages

Supplier and Parts Database Queries

The document outlines the structure and creation of three database tables: suppliers, parts, and catalog, along with their relationships. It includes SQL commands for inserting data into these tables and provides a series of queries to retrieve specific information about suppliers and parts. The queries cover various aspects such as supplier-part relationships, pricing, and color categorization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Queries on Suppliers Database:

Suppliers(sid, sname, address)

Parts(pid, pname, color)

Catalog(sid, pid, cost)

Supplier Table:

CREATE TABLE suppliers(id INTEGER,sname VARCHAR(256),address


VARCHAR(256),CONSTRAINT suppliers_pk PRIMARY KEY (id));

Table Created

Parts Table:

CREATE TABLE parts (

id INTEGER,

pname VARCHAR(256),

color VARCHAR(100),

CONSTRAINT parts_pk PRIMARY KEY (id)

);

Catalog Table:

CREATE TABLE catalog (

sid INTEGER,

pid INTEGER,

cost NUMERIC(10,2),

CONSTRAINT catalog_pk PRIMARY KEY(sid,pid),

CONSTRAINT catalog_sid_fk FOREIGN KEY(sid) REFERENCES


suppliers(id),

CONSTRAINT catalog_pid_fk FOREIGN KEY(pid) REFERENCES


parts(id));
Table Created

Insert Values into Tables:

INSERT INTO parts VALUES (1, 'Left Handed Bacon Stretcher Cover', 'Red');

INSERT INTO parts VALUES (2, 'Smoke Shifter End', 'Black');

INSERT INTO parts VALUES (3, 'Acme Widget Washer', 'Red');

INSERT INTO parts VALUES (4, 'Acme Widget Washer', 'Silver');

INSERT INTO parts VALUES (5, 'I Brake for Crop Circles Sticker',
'Translucent');

INSERT INTO parts VALUES (6, 'Anti-Gravity Turbine Generator', 'Cyan');

INSERT INTO parts VALUES (7, 'Anti-Gravity Turbine Generator',


'Magenta');

INSERT INTO parts VALUES (8, 'Fire Hydrant Cap', 'Red');

INSERT INTO parts VALUES (9, '7 Segment Display', 'Green');

INSERT INTO suppliers VALUES (1, 'Acme Widget Suppliers', '1 Grub St.,
Potemkin Village, IL 61801');

INSERT INTO suppliers VALUES (2, 'Big Red Tool and Die', '4 My Way,
Bermuda Shorts, OR 90305');

INSERT INTO suppliers VALUES (3, 'Perfunctory Parts', '99999 Short Pier,
Terra Del Fuego, TX 41299');

INSERT INTO suppliers VALUES (4, 'Alien Aircaft Inc.', '2 Groom Lake,
Rachel, NV 51902');

INSERT INTO catalog VALUES (1, 3, 0.50);

INSERT INTO catalog VALUES (1, 4, 0.50);

INSERT INTO catalog VALUES (1, 8, 11.70);

INSERT INTO catalog VALUES (2, 3, 0.55);

INSERT INTO catalog VALUES (2, 8, 7.95);

INSERT INTO catalog VALUES (2, 1, 16.50);


INSERT INTO catalog VALUES (3, 8, 12.50);

INSERT INTO catalog VALUES (3, 9, 1.00);

INSERT INTO catalog VALUES (4, 5, 2.20);

INSERT INTO catalog VALUES (4, 6, 1247548.23);

INSERT INTO catalog VALUES (4, 7, 1247548.23);

Retrieve Queries:

1) Find the names of parts for which there is no supplier.

2) Find the names of suppliers who supply every part.

3) Find the names of suppliers who supply every red part.

4) Find the names of parts supplied by Acme Widget Suppliers and no one else

5) Find the IDs of suppliers who charge more for some part than the average
cost of that part (average over all suppliers who supply that part)

6) For each part, find the name of the supplier who charges the most for that
part.

7) Find the IDs of suppliers who do not sell any non-red parts

8) Find the IDs of suppliers who sell a red part and a green part.

9) Find the IDs of suppliers who sell a red part or a green part.

10) For every supplier that only supplies green parts, print the name of the
supplier and the total number of parts that she supplies.

11) For every supplier that supplies a green part and a red part,print both the
name and price of the most expensive part that she supplies and the name and
price of the least expensive part that she supplies.

1) Find Sids of suppliers who supply every red part

2) Find Sids of suppliers who supply every part that is either red or green part

3) Find Sids of suppliers who supply every red part or every green part.
4) Find the average price of red parts

5) For each color, find the number of parts with that color.

6) For each color, find the number of parts with that color, provided that the
average cost (of parts with that color) is at least 40.

7) Find the number of parts that are not supplied by any supplier

8) Find the total number of tuples in all three tables

9) Find the average number of tuples per table in the three database tables

10) Find names of suppliers who supply at least 6 different parts

11) Find all suppliers who supply a part with an unknown (i.e., null) color.

12) For each supplier, return the number of parts supplied by that supplier,
which have an unknown (i.e., null) color.

13) Find the names of all parts whose color starts with the letter b.

14) For each part in Catalog, return its pid, along with the cheapest price and the
most expensive price, for which it is supplied

15) For each part in Part, return pid, along with the cheapest price and the most
expensive price, for which it is supplied. If the part is not supplied, then the
cheapest and most expensive prices should be null.

Common questions

Powered by AI

The query to retrieve this information is: SELECT p.color, COUNT(*) FROM parts p JOIN catalog c ON p.id = c.pid GROUP BY p.color HAVING AVG(c.cost) > 40. This provides the count of parts for each color where the average cost is greater than 40 .

The SQL query for this would be: SELECT DISTINCT s.sid FROM catalog s JOIN (SELECT pid, AVG(cost) AS avg_cost FROM catalog GROUP BY pid) av ON s.pid = av.pid WHERE s.cost < av.avg_cost. This query calculates the average cost per part and selects distinct supplier IDs where the supplier's cost is less than the average .

The query is: (SELECT COUNT(*) FROM suppliers) + (SELECT COUNT(*) FROM parts) + (SELECT COUNT(*) FROM catalog). This sums up the tuple counts from each table to determine the total number of tuples across the database .

Use: SELECT p.pname FROM parts p WHERE EXISTS (SELECT * FROM catalog c WHERE c.pid = p.id AND c.sid = 1) AND NOT EXISTS (SELECT * FROM catalog c WHERE c.pid = p.id AND c.sid <> 1). This finds part names supplied by Acme Widget Suppliers and ensures no other supplier provides them .

To find the names of suppliers who supply every red part, you can use the following SQL query: SELECT s.name FROM suppliers s WHERE NOT EXISTS (SELECT * FROM parts p WHERE p.color = 'Red' AND NOT EXISTS (SELECT * FROM catalog c WHERE c.sid = s.id AND c.pid = p.id)). This query checks for any suppliers who have not supplied any red parts, and returns the names of those who have supplied all red parts .

The query is: SELECT p.id FROM parts p WHERE NOT EXISTS (SELECT * FROM catalog c WHERE c.pid = p.id). This checks for parts with no corresponding entry in the catalog table, indicating they are not supplied .

The SQL query for this task is: SELECT s.name FROM suppliers s JOIN catalog c ON s.id = c.sid GROUP BY s.name HAVING COUNT(DISTINCT c.pid) >= 6. This groups suppliers by name and checks if they supply parts of at least six different types .

To find IDs of suppliers who sell only red parts, the query is: SELECT s.sid FROM suppliers s WHERE NOT EXISTS (SELECT * FROM catalog c JOIN parts p ON c.pid = p.id WHERE c.sid = s.id AND p.color <> 'Red'). This checks for the non-existence of non-red parts sold by any supplier .

The SQL query is: SELECT AVG(c.cost) FROM catalog c JOIN parts p ON c.pid = p.id WHERE p.color = 'Red'. This calculates the average cost of all parts in the color red by joining the catalog with parts filtered by red color .

For this, the SQL query would be: SELECT c.pid, MAX(c.sid), MAX(c.cost), MIN(c.sid), MIN(c.cost) FROM catalog c GROUP BY c.pid. This looks at each part in the catalog and finds the respective suppliers who offer the highest and lowest cost .

You might also like