Question

SQL

I have a database

CREATE TABLE vendor

( vid CHAR(2) NOT NULL,

vname VARCHAR(25) NOT NULL,

PRIMARY KEY (vid) );

CREATE TABLE category

( catid CHAR(2) NOT NULL,

catname VARCHAR(25) NOT NULL,

PRIMARY KEY (catid) );

CREATE TABLE product

( pid CHAR(3) NOT NULL,

pname VARCHAR(25) NOT NULL,

price NUMERIC (7,2) NOT NULL,

vid CHAR(2) NOT NULL,

categoryid CHAR(2) NOT NULL,

PRIMARY KEY (pid));

CREATE TABLE region

( rid CHAR NOT NULL,

rname VARCHAR(25) NOT NULL,

PRIMARY KEY (rid) );

CREATE TABLE store

( sid VARCHAR(3) NOT NULL,

szip CHAR(5) NOT NULL,

rid CHAR NOT NULL,

PRIMARY KEY (sid));

CREATE TABLE customer

( cid CHAR(7) NOT NULL,

cname VARCHAR(15) NOT NULL,

czip CHAR(5) NOT NULL,

gender CHAR(1) NOT NULL,

PRIMARY KEY (cid) );

CREATE TABLE salestrans

( tid VARCHAR(8) NOT NULL,

custid CHAR(7) NOT NULL,

storeid VARCHAR(3) NOT NULL,

sdate VARCHAR(10) NOT NULL,

PRIMARY KEY (tid));

CREATE TABLE soldvia

( prodid CHAR(3) NOT NULL,

transid VARCHAR(8) NOT NULL,

numitems INT NOT NULL,

PRIMARY KEY (prodid, transid));

INSERT INTO vendor VALUES ('PG','Pacifica Gear');

INSERT INTO vendor VALUES ('MK','Mountain King');

INSERT INTO category VALUES ('CP','Camping');

INSERT INTO category VALUES ('FW','Footwear');

INSERT INTO product VALUES ('1X1','Zzz Bag',100,'PG','CP');

INSERT INTO product VALUES ('2X2','Easy Boot',70,'MK','FW');

INSERT INTO product VALUES ('3X3','Cosy Sock',15,'MK','FW');

INSERT INTO product VALUES ('4X4','Dura Boot',90,'PG','FW');

INSERT INTO product VALUES ('5X5','Tiny Tent',150,'MK','CP');

INSERT INTO product VALUES ('6X6','Biggy Tent',250,'MK','CP');

INSERT INTO product VALUES ('7X7','Comfy Pad',50,'PG','CP');

INSERT INTO product VALUES ('8X8','Turkey Jerky',2,'PG','CP');

INSERT INTO region VALUES ('C','Chicagoland');

INSERT INTO region VALUES ('T','Tristate');

INSERT INTO region VALUES ('D','Downstate');

INSERT INTO region VALUES ('O','OutOfstate');

INSERT INTO store VALUES ('S1','60600','C');

INSERT INTO store VALUES ('S2','60605','C');

INSERT INTO store VALUES ('S3','35400','T');

INSERT INTO customer VALUES ('1-2-333','Tina','60137', 'F');

INSERT INTO customer VALUES ('2-3-444','Tony','60611', 'M');

INSERT INTO customer VALUES ('4-5-666','Pam ','35401', 'F');

INSERT INTO salestrans VALUES ('T111','1-2-333','S1','1/1/2011');

INSERT INTO salestrans VALUES ('T222','2-3-444','S2', '1/1/2011');

INSERT INTO salestrans VALUES ('T333','1-2-333','S3', '2/1/2011');

INSERT INTO salestrans VALUES ('T444','4-5-666','S3', '2/1/2011');

INSERT INTO salestrans VALUES ('T555','2-3-444','S3', '2/1/2011');

INSERT INTO soldvia VALUES ('1X1','T111',1);

INSERT INTO soldvia VALUES ('2X2','T222',1);

INSERT INTO soldvia VALUES ('3X3','T333',5);

INSERT INTO soldvia VALUES ('1X1','T333',1);

INSERT INTO soldvia VALUES ('4X4','T444',1);

INSERT INTO soldvia VALUES ('2X2','T444',2);

INSERT INTO soldvia VALUES ('4X4','T555',4);

INSERT INTO soldvia VALUES ('5X5','T555',2);

INSERT INTO soldvia VALUES ('6X6','T555',1);

1. List the name and ID of customers who made a sales transaction on 2/1/2011 a) b) c) Write this query using the in or a quantified predicate. (10pts) Write this query using the exists predicate. (10pts) Write this query as a single block query. (10pts) 2. List the customer name and store zipcode of all female customers who made a sales transaction at a store in the region named Chicagoland. (10pts) 3. List the customer name and customer zipcode of all customers who did not buy any items from store S2. a) Write this query using a nested query. (10pts) b) Write this query using the EXCEPT operation (Oracle uses minus). (10pts) 4. Display the total number of products that cost less than $100. (10pts) 5. For each vendor, display the vendor name and the cost of the product with the lowest cost in category CP. (10pts) 6. Retrieve the TID and total number of products sold for each transaction that has more than 1 product. (10pts) 7. List the region name, store id and store zip for all regions. Include all regions, even those regions without any stores. (10pts)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

a)

The SQL query to display name and id of the customers who made a transaction on 2/1/2011 using ‘in’ is given as follows:

SELECT cname, cid FROM customer, salestrans where customer.cid = salestrans.custid and sdate in ('2/1/2011');


Number of Records: 3 cnane Tina Pam Tony cid 1-2-333 4-5-666 2-3-444

b)

The SQL query to display name and id of the customers who made a transaction on 2/1/2011 using ‘exists’ is given as follows:

SELECT cname, cid FROM customer where exists (select custid from salestrans where custid = customer.cid and sdate = '2/1/2011');


c)

The SQL query to display name and id of the customers who made a transaction on 2/1/2011 using single block query is given as follows:

SELECT cname, cid FROM customer, salestrans where customer.cid = salestrans.custid and sdate = '2/1/2011';


2.

The SQL query to display the customer name and store zip code of all female customers who made a sales transaction at a store in the region named ‘Chicagoland’ is given as follows:

SELECT cname, szip FROM customer join salestrans on salestrans.custid = customer.cid join store on store.sid = salestrans.storeid join region on region.rid = store.rid where rname = 'Chicagoland' and gender = 'F';


3.

a)

The SQL query to display the customer name and customer zip code of all customers who did not buy any items from store S2 using nested query is given as follows:

select cname, czip from customer where cname <> (select cname from customer, salestrans where customer.cid = salestrans.custid and storeid = 'S2');


b)

The SQL query to display the customer name and customer zip code of all customers who did not buy any items from store S2 using nested query is given as follows:

select cname, czip from customer, salestrans except select cname, czip from customer, salestrans where customer.cid = salestrans.custid and salestrans.storeid = 'S2';


4.

The SQL query to display the total number of products that cost less than $100 is given as follows:

select count(price) as NumberOfProducts from product where price < 100;


5.

The SQL query to display the vendor name and cost of product with the lowest cost in category CP is given as follows:

select vname, price, min(price) as MinPrice from vendor, product where vendor.vid = product.vid and categoryid = 'CP';


6.

The SQL query to display the TID and total number of products sold for each transaction that has more than 1 product is given as follows:

select tid, count(pid) as TotalProduct from product, salestrans, soldvia where salestrans.tid = soldvia.transid and soldvia.prodid = product.pid and numItems > 1;


7.

The SQL query to display the region name, store id, and store zip for all regions even those regions which do not have any stores is given as follows:

select rname, sid, szip from region left outer join store on region.rid = store.rid;

Add a comment
Know the answer?
Add Answer to:
SQL I have a database CREATE TABLE vendor ( vid CHAR(2) NOT NULL, vname VARCHAR(25) NOT...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Database Management 6. [5] Create the following table: CREATE TABLE customer ( cust_name VARCHAR(30) NOT NULL,...

    Database Management 6. [5] Create the following table: CREATE TABLE customer ( cust_name VARCHAR(30) NOT NULL, address VARCHAR(60), UNIQUE (cust name, address)); A. Run the following inserts and explain why both work and how will you prevent it INSERT INTO customer VALUES ('Alex Doe', NULL); INSERT INTO customer VALUES ('Alex Doe', NULL); 7. [5] Modify the following table definition to ensure that all employees have a minimum wage of $10 CREATE TABLE employee ( empId INT PRIMARY KEY, empName VARCHAR(40)...

  • CREATE TABLE DEPT ( DEPTNO INTEGER NOT NULL, DNAME VARCHAR(14), LOC VARCHAR(13), PRIMARY KEY (DEPTNO)); INSERT...

    CREATE TABLE DEPT ( DEPTNO INTEGER NOT NULL, DNAME VARCHAR(14), LOC VARCHAR(13), PRIMARY KEY (DEPTNO)); INSERT INTO DEPT VALUES (10,'SPORTS','NEW YORK'); INSERT INTO DEPT VALUES (20,'HOME','DALLAS'); INSERT INTO DEPT VALUES (30,'OUTDOOR','CHICAGO'); INSERT INTO DEPT VALUES (40,'CLOTHING','BOSTON'); CREATE TABLE EMP ( EMPNO INTEGER NOT NULL, ENAME VARCHAR(10), JOB VARCHAR(9), MGR INTEGER, SAL FLOAT, COMM FLOAT, DEPTNO INTEGER NOT NULL, FOREIGN KEY (DEPTNO) REFERENCES DEPT (DEPTNO), FOREIGN KEY (MGR) REFERENCES EMP(EMPNO), PRIMARY KEY (EMPNO)); INSERT INTO EMP VALUES (7839,'KING','PRESIDENT',NULL, 5000,NULL,10); INSERT INTO...

  • Relation Students has schema: CREATE TABLE Students (     BannerID CHAR(9),     stuName VARCHAR(40) NOT NULL,...

    Relation Students has schema: CREATE TABLE Students (     BannerID CHAR(9),     stuName VARCHAR(40) NOT NULL,     scholarship INT,     PRIMARY KEY(BannerID)); The relation Students is currently empty. Develop a test that determines whether an insertion into Students is currently legal. Then apply your test to determine which of the following INSERT statements is allowable.             a. INSERT INTO Students VALUES(950111333, ’John Smith’, 1000); b. INSERT INTO Students (BannerID, stuName) VALUES(‘950111333’, ’John Smith’); c. INSERT INTO Students VALUES(‘950111222’, NOT NULL,...

  • CREATE TABLE vendor ( vendor_id int NOT NULL, vendor_name char(50) NOT NULL, contact_name char(50), CONSTRAINT vendors_pk...

    CREATE TABLE vendor ( vendor_id int NOT NULL, vendor_name char(50) NOT NULL, contact_name char(50), CONSTRAINT vendors_pk PRIMARY KEY (vendor_id) ); What is the code to add the foreign key for this table? Can you please add the entire code with mine included. Thank you in advance.

  • -- Schema definition create table Customer (     cid   smallint not null,     name varchar(20),    ...

    -- Schema definition create table Customer (     cid   smallint not null,     name varchar(20),     city varchar(15),     constraint customer_pk         primary key (cid) ); create table Club (     club varchar(15) not null,     desc varchar(50),     constraint club_pk         primary key (club) ); create table Member (     club varchar(15) not null,     cid   smallint     not null,     constraint member_pk         primary key (club, cid),     constraint mem_fk_club         foreign key (club) references Club,     constraint mem_fk_cust...

  • Create a stored procedure named prc_inv_amounts to update the INV_SUBTOTAL, INV_TAX, and INV_TOTAL. The procedure takes the invoice number as a parameter. The INV_SUBTOTAL is the sum of the LINE_TOTAL...

    Create a stored procedure named prc_inv_amounts to update the INV_SUBTOTAL, INV_TAX, and INV_TOTAL. The procedure takes the invoice number as a parameter. The INV_SUBTOTAL is the sum of the LINE_TOTAL amounts for the invoice, the INV_TAX is the product of the INV_SUBTOTAL and the tax rate (8 percent), and the INV_TOTAL is the sum of the INV_SUBTOTAL and the INV_TAX /* Database Systems, Coronel/Morris */ /* Type of SQL : SQL Server */ /* WARNING: */ /* WE HIGHLY RECOMEND...

  • I need help with the following SQL query for a company database (script given below). The...

    I need help with the following SQL query for a company database (script given below). The name of the Department. The number of employees working in that department. The number of different projects controlled by this department. The name of the project controlled by this department that has the maximum number of employees of the company working on it. The number of the above project. The cumulative sum of the number of employees of the company working on the projects...

  • please answer 56789 CREATE TABLE ALLDRINKS(   /* All legal drinks */ DRINK       VARCHAR(30)   NOT NULL,  ...

    please answer 56789 CREATE TABLE ALLDRINKS(   /* All legal drinks */ DRINK       VARCHAR(30)   NOT NULL,   /* Drink name   */    CONSTRAINT DRINKNAME_PKEY PRIMARY KEY(DRINK) ); CREATE TABLE DRINKERS ( /* All drinkers */ DRINKER   VARCHAR(30)   NOT NULL,    CONSTRAINT DRINKERS_PKEY PRIMARY KEY (DRINKER)); CREATE TABLE LOCATED(   /* Pubs have locations */ PUB           VARCHAR(30)   NOT NULL,   /* Pub name   */ STREET       VARCHAR(30)   NOT NULL,   /* Street name   */ BLDG_NO       DECIMAL(4)   NOT NULL,   /* Building number   */...

  • MICROSOFT SQL SERVER - Create the following views 1.List the employee assigned to the most projects...

    MICROSOFT SQL SERVER - Create the following views 1.List the employee assigned to the most projects 2.List the project with the most hours SCRIPT TO BUILD DATABASE create table department ( dept_ID int not null , dept_name char(50) NOT NULL, manager_ID int not null, manager_start_date date not null, constraint Dept_PK primary key (dept_ID), constraint D_Name_AK unique (dept_name) ); insert into department values(1,'abc',1,'2019-01-08'); insert into department values(2,'abc2',2,'2019-01-08'); insert into department values(3,'abc3',2,'2019-01-08'); insert into department values(4,'abc4',2,'2019-01-08'); /*project table done*/ create table project...

  • * myCompany.SQL Introduction to SQL Script file for ORACLE DBMS This script file creates the following...

    * myCompany.SQL Introduction to SQL Script file for ORACLE DBMS This script file creates the following tables: VENDOR, PRODUCT, CUSTOMER, INVOICE, LINE EMPLOYEE and loads the default data rows */ set echo on; set serveroutput on; select systimestamp from dual; show user; ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY'; DROP TABLE LINE CASCADE CONSTRAINTS; DROP TABLE INVOICE CASCADE CONSTRAINTS; DROP TABLE CUSTOMER CASCADE CONSTRAINTS; DROP TABLE PRODUCT CASCADE CONSTRAINTS; DROP TABLE VENDOR CASCADE CONSTRAINTS; DROP TABLE EMPLOYEE CASCADE CONSTRAINTS; CREATE TABLE...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT