This is extra information about the shopping database given to answer this question:
For many query questions we refer to the following database schema for a website that keeps track of what people like to buy or actually buy in different on-line supermarkets. (Think of something like a cross-store loyalty card system.)
customer(cID, cName, street, city)
Customers with unique cID and other attributes
store(sID, sName, street, city)
Stores with a unique ID and other attributes. Stores with the same name but different sID (and address) form what we call a store chain.
product(pID, pName, suffix)
Products with a unique ID
shoppinglist(cID, pID, quantity, date)
What a customer wants to buy on a certain date. (Every customer creates at most one shoppinglist per day.)
purchase(tID, cID, sID, pID, date, quantity, price)
A transaction: what a customer bought in one go;
every transaction (tID) corresponds to one customer (cID) buying
from one store (sID) on one date (date); at some point during the
course we see that this actually means tID → cID,sID,date
inventory(sID, pID, date, quantity, unitPrice)
Inventory of a store as published at the start of a date.
Primary key attributes are underlined. (Foreign keys are not indicated but can be inferred from the attribute names that have been used.)
The database is "complete" (closed world assumption): we do not consider anything that may exist outside the database (like people who are not a customer or products that nobody wants and no store sells).
All customers appear in customer, all products in product and all stores in store. A store name that appears only once does not constitute a store chain. A store chain requires the existence of at least two stores with the same name.
When a store carries (sells) a product it may temporarily have an inventory quantity of 0 (meaning the product is out of stock). The product is then not removed from inventory. It is possible that there are several products (different pID) with the same pName and suffix, due to different identification schemes.
Although the true inventory changes constantly during the day the published inventory is only updated once, at the start of the day. (At some point during the course we start dealing with this issue.)
The question is:
Consider the shopping database schema.
Write a SQL query for the following natural language query.
Find all the different store ids that only sold products to people that live in same city as the location of the store.
Please use the below query.
Initially I have created customer table,store table and enter some sample value.
Query(customer table):
create table customer(cid integer, cname varchar(100),street varchar(100),city varchar(100));
insert into customer values(1, "Anirudh","South Street", "Chennai");
insert into customer values(2, "Anil","New Street", "Mumbai");
insert into customer values(3, "Rokhit","Gandhiji Road", "New Jersy");
select * from customer;
Customer table:
cid | cname | Street | city |
1 | Anirudh | South Street | Chennai |
2 | Anil | New Street | Mumbai |
3 | Rokhit | Gandhiji Road | New Jersy |
Query(store table):
create table store(sid integer, sname varchar(100),street varchar(100),city varchar(100));
insert into store values(1, "New Trendy","Gandhiji Street", "New Jersy");
insert into store values(2, "Fashion","West Street", "Chennai");
insert into store values(3, "Trendy","New Street", "Hyderabad");
select * from store;
Store table:
sid | sname | street | city |
1 | New Trendy | Gandhiji Street | New Jersy |
2 | Fashion | West Street | Chennai |
3 | Trendy | New Street | Hyderabad |
Question:
Find all the different store ids that only sold products to people that live in same city as the location of the store.
I have stored the below data in the purchase table.
tid | cid | sid | pid | date | quantity | price |
1 | 3 | 1 | 2 | 12/4/2019 | 2 | 90 |
2 | 1 | 3 | 2 | 12/5/2019 | 5 | 990 |
3 | 1 | 2 | 6 | 12/6/2019 | 12 | 890 |
21 | 2 | 3 | 6 | 12/7/2019 | 112 | 8454990 |
use the below query to pull the information from the purchase table.
Query:
select distinct a.sid from purchase a join store b
on a.sid=b.sid
join customer c
on a.cid=c.cid
and b.city=c.city;
Query Explanation:
In the above query i have selected only the sid that is store id by joining the purchase and store table so that we can get the whole purchase table at the first joining a.sid=b.sid.
Then in the second join we have joined with customer table and purchase table based on the customer id. so that we can get the whole purchase table. Finally we have applied the condition b.city=c.city that is nothing but we have compared the location of customer and store table. so it returned only two rows.
purchase | ||||||
tid | cid | sid | pid | date | quantity | price |
1 | 3 | 1 | 2 | 12/4/2019 | 2 | 90 |
3 | 1 | 2 | 6 | 12/6/2019 | 12 | 890 |
The first row has the customer id 3. From the customer table the location is New Jersy and sid 1 from the store table the location is also New Jersy. In the same way customer id 1 and store id 2 has the same location Chennai. Hence it returned only the store and customer has same location.
Output:
sid |
2 |
1 |
This is extra information about the shopping database given to answer this question: For many query...
Given the following relational schema, write queries in SQL to answer the English questions. There is a shipment database on the MySQL server. You can also use the DDL for MySQL. You must only submit the SQL for your answers but you can include the query output as well to help the TA with marking. Customer(cid: integer, cname: string, address: string, city: string, state: string) Product(pid: integer, pname: string, price: currency, inventory: integer) Shipment(sid: integer, cid: integer, shipdate: Date/Time) ShippedProduct(sid:...
Given the following relational schema, write queries in SQL to answer the English questions. There is a shipment database on the MySQL server. You can also use the DDL for MySQL. You must only submit the SQL for your answers but you can include the query output as well to help the TA with marking. Customer(cid: integer, cname: string, address: string, city: string, state: string) Product(pid: integer, pname: string, price: currency, inventory: integer) Shipment(sid: integer, cid: integer, shipdate: Date/Time) ShippedProduct(sid:...
Given the following relational schema, write queries in SQL to answer the English questions. The Access Database for the schema is available, as is a DDL file. It is also available on the MySQL server. You must only submit the SQL for your answers. You can get your answers without using a DBMS or by using Access or MySQL. Customer(cid: integer, cname: string, address: string, city: string, state: string) Product(pid: integer, pname: string, price: currency, inventory: integer) Shipment(sid: integer, cid:...
A database used for a toy store that keeps track of the inventory and purchase orders from the store. All clients will have their membership ID card to shop. This database will help the store to keep track of their orders. It will also help the store to know how many products are available in the inventory. It is necessary to have a membership to purchase a product. The customer can also bring coupons to get discount. Key attributes: Salesman:...
A database used for a toy store that keeps track of the inventory and purchase orders from the store. All clients will have their membership ID card to shop. This database will help the store to keep track of their orders. It will also help the store to know how many products are available in the inventory. It is necessary to have a membership to purchase a product. The customer can also bring coupons to get discount. Key attributes: Salesman:...
7. Using the provided schema of a Purchase Order Administration database, write the following queries in SQL. (In the schema, bold attributes are primary keys and italicized attributes are foreign keys.) SUPPLIER (SUPNR, SUPNAME, SUPADDRESS, SUPCITY, SUPSTATUS) SUPPLIES (SUPNR, PRODNR, PURCHASE_PRICE, DELIV_PERIOD) PRODUCT (PRODNR, PRODNAME, PRODTYPE, AVAILABLE_QUANTITY) PO_LINE (PONR, PRODNR, QUANTITY) PURCHASE_ORDER (PONR, PODATE, SUPNR) 7d) Write a nested SQL query to retrieve the supplier number, supplier name, and supplier status of each supplier who has a higher supplier status...
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)...
You will develop an E-Commerce database used to maintain customers, products and sales information. You are required to 1) gather and analyze requirements 2) design logical structure of the database 3) create stored procedures to develop the tables and insert the data 4) write SQL statements for data extraction and reporting. Throughout the course of this semester you have analyzed the requirements for an eCommerce database, designed and developed your database. As a class we have gone through the process...
The following relational schema is given for a Pokemon Database: Pokemon (PID, PName) Type (TID, TName) Trainer (SSN, Name, Surname) InType (PID, TID) StrongAgainst (TID_Strong, TID_Weak) Owns (SSN, PID, Nickname, Level, Height, Weight, Catch_Date) SpecializedIn (SSN, TID) In this database, Pokemon (Imaginary creatures, short for “Pocket Monsters”) are stored in the entity set Pokemon, with a unique PID and PName. Pokemon types are stored in the entity set Type, with a unique TID and TName. Pokemon trainers are stored in...
Given the following relational database schema (primary keys are bold and underlined). Answer questions 2.1 to 2.4 Orders(orderld, customerld, dateOrdered, dateRequired, status) Customer(customerld, customerLastName, customerStreet, customerCity, customerState, customer Lip OrderDetails(orderld.productld, quantity, lineNumber, amount) Products(productld, name, description, quantity, unitPrice) Account(accountNumber, customerld, dateOpened, creditCard, mailingStreet, mailingCity, mailingState, mailingZip) 2.1 (2 Points) List all possible foreign keys. For each foreign key list both the referencing and referenced relations. 2.2 (2 Points) Devise a reasonable database instance by filling the tables with data of...