0% found this document useful (0 votes)
69 views4 pages

MIS562 Week 2 Assignment: Homework (100 Points)

This document provides instructions for a homework assignment involving SQL queries. Students are asked to: 1) Create tables from a provided SQL script and view the table structures. 2) Write SQL statements to answer multiple choice questions involving selecting data from the tables based on various criteria like department, salary ranges, job titles, etc. 3) Execute another SQL script to add more rows to the tables and write more SQL statements to answer additional questions, including some involving aggregation, subqueries, and inline views. 4) Write SQL queries using other provided tables like ZIPCODES and STUDENT to display requested data like state zip code counts, number of enrollments by student, and more.

Uploaded by

Jp Xtyrael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views4 pages

MIS562 Week 2 Assignment: Homework (100 Points)

This document provides instructions for a homework assignment involving SQL queries. Students are asked to: 1) Create tables from a provided SQL script and view the table structures. 2) Write SQL statements to answer multiple choice questions involving selecting data from the tables based on various criteria like department, salary ranges, job titles, etc. 3) Execute another SQL script to add more rows to the tables and write more SQL statements to answer additional questions, including some involving aggregation, subqueries, and inline views. 4) Write SQL queries using other provided tables like ZIPCODES and STUDENT to display requested data like state zip code counts, number of enrollments by student, and more.

Uploaded by

Jp Xtyrael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MIS562 Week 2 Assignment

Name:

Homework (100 points)

Resources:

- demo.sql

- demoaddrows.sql

Part 1
Create the tables from demo.sql script

Download the demo.sql file from the Doc Sharing. Run demo.sql in MySQL Omnymbus Environment.
This will create some tables and insert data into them. View the script in notepad to determine the table
names that were created. Use the describe command to view the structure of the tables. Please use the
template below to provide your solutions.

Write SQL statements to solve the following requests.

Question (4 pts per question) SQL statement or answer


1. List all employee information in department 30. SELECT ename FROM emp WHERE
deptno=30;
2. List employees name, job, and salary that is a SELECT ename, job, sal FROM emp WHERE
manager and has a salary > $1,000 job=’manager’ AND sal>1000;
3. Repeat exercise 2 for any employee that is not SELECT ename, job, sal FROM emp WHERE
a manager or earns a salary > $1,000 (job=’clerk’,’analyst’,’salesman’) AND
sal>1000;
4. Show all employee names and salary that earn SELECT ename, sal FROM emp WHERE sal
between $1,000 and $2,000. Use the between BETWEEN 1000 AND 2000;
operator.
5. Select all employees that are in department 10 SELECT ename FROM emp WHERE deptno
and 30. Use the IN operator. IN(10,30);
6. Select all employee names with an “A” in the SELECT * FROM emp WHERE ename LIKE
first position of the employee name. Use the ‘A%’;
substring function or a wild card.
7. Select all employees with an “A” as the second SELECT * FROM emp WHERE ename LIKE
character of their name. Use a wildcard. ‘_A%’;
8. List the employee names in alphabetical SELECT ename FROM emp ORDER BY asc;
sequence.

Page 1
MIS562 Week 2 Assignment
9. List the job, salary, and employee name in job SELECT job, sal, ename FROM emp ORDER
order and then salary in descending order. BY job desc, sal desc;
10. Show a list of different jobs. Eliminate repeating SELECT DISTINCT job FROM emp;
values.
11. Show employee names, salary that has not SELECT ename, sal FROM emp WHERE
earned a commission yet. comm IS NULL and comm=0;
12. Show the employee name, salary, commission, SELECT ename, sal, comm, (sal +
and the total of salary and commission. Be coalesce(((sal + comm)/100).0)) “total_sal”
sure you evaluate null values correctly. FROM emp WHERE comm NOT NULL
13. Write an SQL query that retrieves data from the SELECT * FROM course WHERE cost =
COURSE table for courses that cost 1195, and 1195 AND NOT EXISTS (SELECT description
whose descriptions do not start with ‘Intro’, FROM course WHERE description LIKE ‘Intro
sorted by their prerequisites in descending %’) ORDER BY prerequisite desc;
order.
14. Write an SQL query that retrieves data from the SELECT * FROM student WHERE last_name
STUDENT table for students whose last names LIKE ‘[A-T]%’ AND employer=’Competrol Real
begin with “A” though “T” and who work for Estate’, ORDER BY last_name;
‘Competrol Real Estate’, sorted by the last
names.
15. Determine which student does not have the first SELECT student_id, last_name FROM
letter the last name capitalized. Show the student WHERE SUBSTR(last_name,1,1) =
STUDENT_ID and LAST_NAME columns. SUBSTR(LOWER(last_name,1,1);
16. Check if any of the phone numbers in the SELECT phone, instructor_id FROM
INSTRUCTOR table have not been entered in instructor WHERE TRANSLATE(phone,
the (###)###-#### format. Show the instructor ‘0123456789 ‘,’#########’);
last name and the phone number that is in the
incorrect format.

17. Write an SQL statement that uses the CAST CAST ($187.20 AS VARCHAR(7));
function that converts a number datatype to a
varchar datatype.
18. Write a SQL statement that converts a date CAST (01-10-2012 AS CHAR(10));
datatype to a char datatype.

19. Write a SQL statement that convert a number CAST (12 AS CHAR(2));
value to a character.

Part 2
Using the two tables created in Part 1:

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- --------- --------- ------ -------------- ----- -------- ------------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

Page 2
MIS562 Week 2 Assignment
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 30
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
9999 KIRK CAPTAIN 14-APR-02 6000 50

DEPTNO DNAME LOC


---------- -------------- -------------
10 ACCOUNTING NEWYORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
60 STARFLEET SAN FRANCISCO

Execute the script demoaddrows.sql to add rows into the two tables above. Write SQL statements to
solve the following requests.

Question ( 2 points per question) SQL statement or answer


1. Show a list of different jobs. Eliminate repeating SELECT DISTINCT job FROM emp;
values.
2. How many employees are working at each job SELECT empno, count(empno) FROM emp
in each department and what the sums and HAVING sum(sal) AND avg(sal);
averages are for the salary of those employees?
3. Show the employee name with the maximum SELECT ename FROM emp WHERE emp
salary HAVING max(sal);
4. Show the average salary for all employees that SELECT sal FROM emp WHERE
are not managers. job=’clerk’,’analyst’,’salesman’ HAVING
avg(sal);
5. What is the difference between the highest and SELECT MAX(sal) – MIN(sal) FROM emp;
lowest salary?
6. Select employee number, name for all SELECT empno, ename FROM emp WHERE
employees with salaries above the average sal > (SELECT AVG(sal) FROM emp);
salary. Use a subquery.

Using the ZIPCODES table from the text schema, write a SQL query that displays the following:
Question ( 2 points per question) SQL statement or answer
7. Show the state and the number of zip codes SELECT state, COUNT(DISTINCT zip_codes)
by state. Order the result by number of zip FROM zipcodes ORDER BY zip_codes desc;
codes in descending sequence. Use the
ZIPCODES table.
8. Which city has the most zip codes assigned to SELECT zipcode, COUNT(zipcode), city

Page 3
MIS562 Week 2 Assignment
it? Use a TOP-N query. FROM zipcode GROUP BY zipcode, city
ORDER BY city;
9. Show the state and the number of zip codes SELECT zipcode, COUNT(zipcode), state
by state. Order the result by number of zip FROM zipcode GROUP BY zipcode, state
codes in descending sequence and exclude ORDER BY zipcode desc HAVING zipcode >
all states having less than 5 zip codes.
5;

Using the text schema, write a SQL query that displays the following:
Question ( 2 points per question) SQL statement or answer
10. Show the Student_ID, last name, and the SELECT Student_ID, last_name, enrollments
number of enrollments for the student. Show FROM (SELECT Student_ID,SUM(enrollement)
only students with more than 2 enrollments. FROM student GROUP BY Student_ID
Use an Inline view. HAVING SUM(enrollments) >2) student;
11. Show the number of students enrolled and zip SELECT city, zipcode, (SELECT
code for New York and where the city begins COUNT(student_ID) FROM students WHERE
with 'W'. Use a Scalar subquery. state = New York AND city = ‘W%’) AS
“Students Enrolled” FROM students;
12. Display the course number and description of SELECT course_no, description FROM course
courses with no enrollment. Also include c WHERE NOT EXISTS (SELECT NULL FROM
courses which have no section assigned. section s WHERE c.course_no=s.course_no)
OR course_no IN (SELECT course_no FROM
section s2 WHERE NOT EXISTS (SELECT
NULL from enrollment e WHERE s2.section_id
= e.section_id));

Page 4

You might also like