Question

In: Computer Science

Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) In a plain...

Consider the following schema:

Suppliers(sid, sname, address)

Parts(pid, pname, color)

Catalog(sid, pid, cost)

In a plain text file, write the following queries in SQL:

1.Find the names of all suppliers who supply a green part.

2.Find the names of all suppliers who are from Illinois.

3.Find the names of all suppliers who sell a red part costing less than $100.

4.Find the names and colors of all parts that are green or red.

In writing these (basic) queries you may make the following assumptions:

a. Each part has only one color.
b. The cost field is a real number with two decimal places (e.g., 100.25, 93.00).
c. The sid field in Suppliers and the sid field in Catalog refer to the same field.
d. The pid field in Parts and the pid field in Catalog refer to the same field.

Solutions

Expert Solution

Answer:

Creating Suppliers Table:

create table suppliers(sid int primary key,sname char(20),address char(20));

Creating Parts Table:

create table parts(pid int primary key,pname char(20),color char(20));

Creating catalog table:

create table catalog (sid int,pid int,cost float);

Adding Foreign keys :

alter table catalog add foreign key (sid) references suppliers(sid);

alter table catalog add foreign key (pid) references parts(pid);

Inserting values into Tables:

insert into suppliers values (1,'John','Illinos'),(2,'Ding','Godrich'),(3,'Rom','Illinos'),(4,'Adam','YorkShire');

insert into parts values (100,'Bag','Brown'),(101,'Handbag','Green'),(102,'Laptop','Red'),(103,'Mobile','Green');

insert into catalog values (1,100,100.25),(2,101,90.50),(3,100,110.50),(2,102,80),(4,102,90),(3,103,101);

Displaying data from tables:

select * from suppliers;

select * from parts;

select * from catalog;

Query 1:

select a.sname from suppliers a,parts b ,catalog c where a.sid=c.sid and b.pid=c.pid and b.color='green';

Explanation: Here we are retrieving names of suppliers who are supplying green part for that we are checking the sid in suppliers and sid in catalog and pid in parts and pid in catalog and the color of part is green.

Query 2:

select sname from suppliers where address='illinos'

Explanation : Here we are retrieving the names of suppliers whose address is illinos by checking address

Query 3:

select a.sname from suppliers a,parts b ,catalog c where a.sid=c.sid and b.pid=c.pid and b.color='red' and c.cost<100.00;

Explanation :

In this query we are retrieving the names of suppliers who are supplying red parts and that part cost is less than 100 .for that we are joining three tables and checking sids and pids.

Query 4:

select pname,color from parts where color='green' or color='red';

Explanation:

Here by using or operator we are retrieving the part names and their color if the color of part is either red or green.

Queries Screenshots and Outputs:

Note : if you have any queries please post a comment thanks a lot..always available to help you..


Related Solutions

Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) write the following...
Consider the following schema: Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost) write the following queries in SQL: * Find the names of all suppliers who supply a green part. *Find the names of all suppliers who are from Illinois. *Find the names of all suppliers who sell a red part costing less than $100. *Find the names and colors of all parts that are green or red. In writing these queries you may make the following assumptions: a....
Consider the following schema: Suppliers (sid, sname, address) Parts (pid, pname, colour) Catalog(sid, pid, cost) The key for Suppliers is sid, for Parts is pid, and for Catalog is sid and pid The Ca...
Consider the following schema: Suppliers (sid, sname, address) Parts (pid, pname, colour) Catalog(sid, pid, cost) The key for Suppliers is sid, for Parts is pid, and for Catalog is sid and pid The Catalog relation associates prices charged for parts by suppliers. Write the following queries using relational algebra. For items (a) through (e), use the "sequences of assignments" form. For items (f) and (g), use the "expression tree" form. List all assumptions. (Some marks will be given for the quality of your answers.) (a) Find...
INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname:...
INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in SQL using join, nested queries and set operators. 1. Find names of suppliers who supply every red or green part. 2. Find the sids of suppliers who supply every red part or supply every green part. 3. Find sids...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours) Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size) where, Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (year i.e, freshman, sophomore, junior, etc). Courses:...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours)Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size), Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (yeari.e, freshman, sophomore, junior, etc). Courses: All courses have...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname,...
Consider the University Database with the following relations: Professors (pid, pname, dept, ext) Students (sid, sname, major-dept, year) Courses (cid, cname, dept, credithours) Enrollment (sem-year, sid, cid, grade) Teaches (pid, cid, sem-year, class-size) where, Professors: All professors have professor id (pid), name (pname), department that they work (dept), and a phone number extension for their office (ext). Students: All students have id (sid), name (sname), department for their major (major-dept), and a year (year i.e, freshman, sophomore, junior, etc). Courses:...
INTRO TO DATABASES Consider the boat reservation system: Sailors (sid, sname,rating, age) Boats(bid, bname, color) Reserves...
INTRO TO DATABASES Consider the boat reservation system: Sailors (sid, sname,rating, age) Boats(bid, bname, color) Reserves (sid, bid, day) Formulate the following question in relational algebra using two different sequences of relational operators: A. Find the colors of boats reserved by Lubber. B. Find the sids of sailors older than 20 who have not reserved a red boat.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT