Supplier and Parts Database Queries
Supplier and Parts Database Queries
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 .