Question

In: Computer Science

Information on the following Database: create database Sales_Co use Sales_Co create table Vendor (v_code integer, v_name...

Information on the following Database:

create database Sales_Co

use Sales_Co

create table Vendor

(v_code integer,

v_name varchar(35) not null,

v_contact varchar(15) not null,

v_areacode char(3) not null,

v_phone char(8) not null,

v_state char(2) not null,

v_order char(1) not null,

primary key (v_code));

create table product

(p_code varchar(10) constraint product_p_code_pk primary key,

p_descript varchar(35) not null,

p_indate datetime not null,

p_qoh integer not null,

p_min integer not null,

p_price numeric (8,2) not null,

p_discount numeric (4,2) not null,

v_code integer,

constraint product_v_code_fk foreign key (v_code) references vendor);

create table Customer

(cus_code integer primary key,

cus_lname varchar(15) not null,

cus_fname varchar(15) not null,

cus_initial char(1),

cus_areacode char(3),

cus_phone char(8) not null,

cus_balance numeric (9,2) default 0.00,

constraint cus_ui1 unique(cus_lname,cus_fname,cus_phone));

create table Invoice

(inv_number integer primary key,

cus_code integer not null references customer(cus_code),

inv_date datetime default current_timestamp not null,

constraint inv_ck1 check (inv_date > '2010-01-01'));

create table Line

(inv_number integer not null,

line_number numeric(2,0) not null,

p_code varchar(10) not null,

line_units numeric(9,2) default 0.00 not null,

line_price numeric(9,2) default 0.00 not null,

primary key (inv_number,line_number),

foreign key (inv_number) references invoice on delete cascade,

foreign key (p_code) references product(p_code),

constraint line_ui1 unique(inv_number,p_code));

create table Employee

(emp_num integer primary key,

emp_title char(10),

emp_lname varchar(15) not null,

emp_fname varchar(15) not null,

emp_initial char(1),

emp_dob datetime,

emp_hire_date datetime,

emp_years integer,

emp_areacode char(3),

emp_phone char(8));

create table EMP

(emp_num integer primary key,

emp_title char(10),

emp_lname varchar(15) not null,

emp_fname varchar(15) not null,

emp_initial char(1),

emp_dob datetime,

emp_hire_date datetime,

emp_areacode char(3),

emp_phone char(8),

emp_mgr integer);

  1. Formulate SQL queries to run against Sales_Co database.

    1. Use the Employee table in the SaleCo database to prepare a list of all employees whose last names start or begin with ‘Smith’. The list should display employee number, first name, last name, and middle initial. The list should be alphabetized by last name. (5 points)

    2. Prepare a list of all employees who have been with the company for at least 15 years or less than 5 years. Display employee number, first name, last name, employee initial based on first and last names (call this column “Initial”), and number of years the employee has been with the company. Initial column values should have the following format: firstnameinitial.lastnameinitial. For example, the initial for George Kolmycz will be G.K. The list should be arranged from the least number of years to the most. (8 points)

    3. Prepare a list of all customers where customer owe a balance. The list should display customer code, first name, last name, and balance they owe (cus_balance). Arrange the list in ascending order by last name and then descending order by first name. (7 points)

    4. Now, modify the above query to show only the 3rd and 4th rows of your results. (4 points)

    5. Prepare a list of items purchased on each invoice. The list should include invoice number, product code of purchased item, quantity purchased, unit price and total amount (units*price) paid for that item. The list should include items from all invoice numbers except 1003, 1006, 1008. (hint: use Line table) (7 points)

    6. Now, modify above query and show top 5 rows of results only. (2 points)

    7. Prepare a list similar to the one in (5) above but include all invoice numbers here. Now, display only those items with total amount paid between $25.00 and $35.00, inclusive. (6 points)

    8. Which invoices were issued to customer # 10014 before January 17, 2018. List invoice number, and date issued. (6 points)

Solutions

Expert Solution

1) Use the Employee table in the SaleCo database to prepare a list of all employees whose last names start or begin with ‘Smith’. The list should display employee number, first name, last name, and middle initial. The list should be alphabetized by last name. (5 points):

Answer:

select emp_num,emp_fname,emp_lname,emp_initial from Employee where emp_lname like 'Smith%' or emp_lname like '%Smith' order by emp_lname;

2)Prepare a list of all employees who have been with the company for at least 15 years or less than 5 years. Display employee number, first name, last name, employee initial based on first and last names (call this column “Initial”), and number of years the employee has been with the company. Initial column values should have the following format: firstnameinitial.lastnameinitial. For example, the initial for George Kolmycz will be G.K. The list should be arranged from the least number of years to the most. (8 points)

Answer:

select emp_num,emp_fname,emp_lname,SUBSTRING(emp_fname,1,1)+SUBSTRING(emp_lname,1,1) as "Initial" ,emp_years from employee order by emp_years asc;

3) Prepare a list of all customers where customer owe a balance. The list should display customer code, first name, last name, and balance they owe (cus_balance). Arrange the list in ascending order by last name and then descending order by first name. (7 points):

Answer:

select cus_code,cus_fname,cus_lname,cus_balance from customer order by cus_lname asc, cus_fname desc;

4)Now, modify the above query to show only the 3rd and 4th rows of your results. (4 points):

Answer:

select cus_code,cus_fname,cus_lname,cus_balance from customer limit 2 offset 2 ;

(Note:Here Limit will allows you to display only 2 values and offset will display the 2 values after 2nd row)

5)Prepare a list of items purchased on each invoice. The list should include invoice number, product code of purchased item, quantity purchased, unit price and total amount (units*price) paid for that item. The list should include items from all invoice numbers except 1003, 1006, 1008. (hint: use Line table) (7 points)

Answer:

select inv_number,p_code,line_units,line_price,(line_units*line_price) as "total amount" from Line where inv_number NOT IN(1003, 1006, 1008);


6) Now, modify above query and show top 5 rows of results only. (2 points):

Answer:

select inv_number,p_code,line_units,line_price,(line_units*line_price) as "total amount" from Line where inv_number NOT IN(1003, 1006, 1008) LIMIT 5;


7)Prepare a list similar to the one in (5) above but include all invoice numbers here. Now, display only those items with total amount paid between $25.00 and $35.00, inclusive. (6 points)

Answer:

select inv_number,p_code,line_units,line_price,(line_units*line_price) as "total_amount" where total_amount between "$25.00" and "$35.00";

8)Which invoices were issued to customer # 10014 before January 17, 2018. List invoice number, and date issued. (6 points)   

Answer:

select inv_number from Invoice where cus_code=10014 and inv_date<"2017-01-17";

​​​​​​​

I hope you find this helpful, if not please comment below i will help you!!

Please do not forget to UpVote the answer!!

Happy Learning!!


Related Solutions

DROP DATABASE class;CREATE DATABASE class;Use class;drop table if exists Class;drop table if exists Student;CREATE TABLE Class...
DROP DATABASE class;CREATE DATABASE class;Use class;drop table if exists Class;drop table if exists Student;CREATE TABLE Class (CIN int PRIMARY KEY, FirstName varchar(255), LastName varchar(255), Gender varchar(1), EyeColor varchar(50), HairColor varchar(50), HeightInches int,CurrentGrade varchar(1));CREATE TABLE Student (SSN int PRIMARY KEY,FirstName varchar(255),LastName varchar(255), Age int,BirthMonth varchar(255),HeightInches int,Address varchar(255),City varchar(255),PhoneNumber varchar(12),Email varchar(255),FavColor varchar(255),FavNumber int);INSERT INTO Class VALUES(1, "David", "San", "M", "BRN", "BLK", 72, "-");INSERT INTO Class VALUES(2, "Jeff", "Gonzales", "M", "BRN", "BLK", 68, "B");INSERT INTO Class VALUES(3, "Anna", "Grayson", "F", "BRN", "BRN", 62,...
Use the following information to create SQL commands to retrieve data from Henry Books database :...
Use the following information to create SQL commands to retrieve data from Henry Books database : For each book, list the book code, book title, publisher code, and publisher name. Order the results by publisher name. For each book published by Plume, list the book code, book title, and price. List the book title, book code, and price of each book published by Plume that has a book price of at least $14. List the book code, book title, and...
Consider the following table definitions create table node( node_id integer primary key, node_color varchar(10)); create table...
Consider the following table definitions create table node( node_id integer primary key, node_color varchar(10)); create table edge( edge_id integer primary key, origin_id integer, destination_id integer, foreign key (origin_id) references node(node_id), foreign key (destination_id) references node(node_id)); What is the result of the following query? select node_id, node_color, destination_id from node, edge; An inner join of the tables node and edge that lists origin node_id and node_color together with the node_id of the destination node for all those nodes that have outgoing...
create table node( node_id integer primary key, node_color varchar(10)); create table edge( edge_id integer primary key,...
create table node( node_id integer primary key, node_color varchar(10)); create table edge( edge_id integer primary key, origin_id integer, destination_id integer, foreign key (origin_id) references node(node_id), foreign key (destination_id) references node(node_id)); write an SQL query that lists all those nodes that have edges with a destination node that has color 'red'.
1.Create a Database in Access with the information The database must include: Database name: Monaco Enterprise  Mark...
1.Create a Database in Access with the information The database must include: Database name: Monaco Enterprise  Mark Johnson #87451 Table name: Contacts Delete the Primary key. Fields name and data type are (remember to choose the data type): Field Name Data Types Employee Name Short text Name Short text Last Name Short Text Work Yes/No 2.Go to the “Datasheet View” and enter the data. * Remember to save the table. 3.Move the last name field after the employee name. 4.The (data)...
Database Design and SQL The following relations keep track of airline flight information: Flights (flno: integer,...
Database Design and SQL The following relations keep track of airline flight information: Flights (flno: integer, from : string, to: string, distance: integer, departs: time, arrive: time, price: integer) Aircraft (aid: integer, aname : string, cruisingrange: integer) Certified (eid: integer, aid: integer) Employees (eid: integer, ename : string, salary: integer) The Employees relation describe pilots and other kinds of employees as well. Every pilot is certified for some aircraft and only pilots are certified to fly. Based on the schemas,...
Your company has created the table below: VENDOR (VENDOR#, VENDOR-NAME, VENDOR-ADDRESS) The company will add other...
Your company has created the table below: VENDOR (VENDOR#, VENDOR-NAME, VENDOR-ADDRESS) The company will add other secondary fields later, but no matter which secondary field it adds, which normal form will this table never violate? Why? VENDOR (VENDOR#, VENDOR-NAME, VENDOR-ADDRESS)
Write create table statements to create tables for the Exoproduct employees computers database depicted by the...
Write create table statements to create tables for the Exoproduct employees computers database depicted by the relational schema created in a mini case MC5 in chapter 3. Use insert into statements to insert no fewer than 2 and no more than 10 records per table.
Create a Database in POSTGRESQL using the following table names and attributes: users: userid (int, primary...
Create a Database in POSTGRESQL using the following table names and attributes: users: userid (int, primary key), name (text) movies: movieid (integer, primary key), title (text) taginfo: tagid (int, primary key), content (text) genres: genreid (integer, primary key), name (text) ratings: userid (int, foreign key), movieid (int, foreign key), rating (numeric), timestamp (bigint, seconds since midnight Coordinated Universal Time (UTC) of January 1, 1970) tags: userid (int, foreign key), movieid (int, foreign key), tagid (int, foreign key), timestamp (bigint, seconds...
Database Systems Lab Exercises Create a table Faculty based on the following chart: Column Data type...
Database Systems Lab Exercises Create a table Faculty based on the following chart: Column Data type Constraints Faculty_Id Number (6) Primary Key => faculty_pk Last_Name Varchar2(15) Not NULL First_Name Varchar2(15) Not NULL Dept Char(3) Save the SQL statement as ex1.sql. Confirm and validate the creation of the new table. Create a table Dept based on the following chart: Column Data type Constraints Dept_Code Char (3) Primary Key => dept_pk Dept_Name Varchar2(20) Not NULL Save the SQL statement as ex2.sql. Confirm...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT