MIS562 Week 2 Assignment: Homework (100 Points)
MIS562 Week 2 Assignment: Homework (100 Points)
Name:
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.
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:
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
Execute the script demoaddrows.sql to add rows into the two tables above. Write SQL statements to
solve the following requests.
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