Functions That Work Across All Data Types

Total Page:16

File Type:pdf, Size:1020Kb

Functions That Work Across All Data Types

SQL Workbook 2 EXERCISES Modified for Microsoft Access Version 7

Draft Version

Copyright Pauline Pollard

1 Contents

How to use the workbooks Workbook 1 introduces you to the functionality of SQL using a small example database. Workbook 2 provides exercises which link to the sections of workbook 1. It is suggested that you work through workbook 1 a section at a time and then carry out the exercises that accompany each section in workbook 2.

Setting up the Database and

Using Access SQL 2

Exercise1a The basic query block 4

Exercise 1b The Where Clause 6

Exercise 2 Functions 9

Exercise 3 Group Functions 10

Exercise 4 Simple Joins 13

Exercise 5 Other join methods 15

Exercise 6 Running Queries with Parameters 17

Exercise 7 Views 18

Exercise 8 Advanced Query Techniques 19

Appendix 1 Setting up the Employee database 20

2 Setting up the Employee Database and Using Access SQL

These instructions tell you how to copy the database that you will need to use for the exercises into your user area. When you have done this you can start working through the workbooks. NB If you do not have access to the server holding the emp database, Appendix 1 describes the tables, column names and field descriptions that are required for the employee database. Appendix 1 assumes a working knowledge of Access.

1. Log in

Press the Keys CTRL + ALT + DEL at the same time At the prompt enter your user name and password Close Windows NT window

2. Start the Windows NT Explorer

Select Start left-hand bottom corner Select Programs Select Windows NT Explorer

3. Copy the emp.mdb file (note the path names given here may change – if the copy function does not work ask your tutor).

Select Data on ‘PB4\Appp’ (I:) Select MscDataM Folder Press the right-hand mouse button on the emp.mdb file and from the menu select copy

4. Copy to your user area

In Windows NT Explorer select your user area Press the right-hand mouse button on file and from the menu select paste Check that the emp. mdb file has copied to your area

5. Open the Microsoft Access program

Select the emp.mdb file and double-click so as to open the file

6. Opening the SQL window and running a query.

From the access menu, create a new query. Select queries and new. Choose design view. When the show table window appears choose close it without selecting a table. This shows the blank query builder window. From here you can toggle to the SQL window by two means. Either select the view menu and then SQL or select the SQL button from the left hand corner of the menu bar. You should now have the SQL window. You can now write your query in the SQL window. To run the query select ! or run.

3 The query builder and SQL

Database tools like Microsoft Access offers two views to build queries: . A Query builder or painter tool . Structured Query Language (SQL)

Fig 1: The painter guides the user through the creation of simple queries.

The painter automatically builds the painter query in Fig.1 as an SQL query: SELECT Emp.ename, Emp.sal, dept.dname FROM Emp INNER JOIN dept ON Emp.deptno = dept.deptno ORDER BY Emp.ename;

SQL, unlike the painter, conforms to a standard and is therefore more portable. Although there remains variations between different products.

Instruction: To get the SQL window and use SQL Use the VIEW menu on the Access tool bar and select SQL view, it is easy to flip between the painter Design View and SQL SQL view. When building a query an edit in one mode will automatically change the other view. To run a query select ! on the tool bar.

4 Exercise1a The basic query block

NB Before you start this exercise you should have set up the employee database and learnt how to open the SQL window.

1. Select all information from SALGRADE table.

grade losal hisal 1 £700.00 £1,200.00 2 £1,201.00 £1,400.00 3 £1,401.00 £2,000.00 4 £2,001.00 £3,000.00 5 £3,001.00 £9,999.00

2. Select employee number, name and department number.

empno ename deptno 7369 SMITH 20 7499 ALLEN 30 7521 WARD 30 7566 JONES 20 7654 MARTIN 30 7698 BLAKE 30 7782 CLARK 10 7788 SCOTT 20 7839 KING 10 7844 TURNER 30 7876 ADAMS 20 7900 JAMES 30 7902 FORD 20 7934 MILLER 10

3. Add £1000 to all employees salary.

updatesal 1800 2600 2250 3975 2250 3850 3450 4000 6000 2500 2100 1950 4000 2300

5 4. List all the different job types.

job ANALYST CLERK MANAGER PRESIDENT SALESMAN

5. List department numbers and names in department name order.

deptno dname 10 Accounting 20 Research 30 Sales 40 Operations

6 Exercise 1b The Where Clause

1. List the name and job of all the analysts.

ename job SCOTT ANALYST FORD ANALYST

6. List the name and department number of all employees in departments 10 and 20 in alphabetical order.

ename deptno ADAMS 20 CLARK 10 FORD 20 JONES 20 KING 10 MILLER 10 SCOTT 20 SMITH 20

7. List names and jobs of all clerks in department 20.

ename job SMITH CLERK ADAMS CLERK

8. a) Display all employees names which have TH in them.

ename SMITH

b) Display all employee names which have a TH or LL in them.

ename SMITH ALLEN MILLER

9. List all employees who have a salary between 1000 and 2000.

empno ename job mgr hiredate sal comm deptno 7499 ALLEN SALESMAN 7698 15-Aug-98 £1,600.00 £300.00 30 7521 WARD SALESMAN 7698 26-Mar-96 £1,250.00 £500.00 30 7654 MARTIN SALESMAN 7698 05-Dec-98 £1,250.00 £1,400.00 30 7844 TURNER SALESMAN 7698 04-Jun-95 £1,500.00 £0.00 30 7876 ADAMS CLERK 7788 04-Jun-99 £1,100.00 20 7934 MILLER CLERK 7782 21-Nov-00 £1,300.00 10

7 8 10. Select all employees who do not have a commission.

ename JONES BLAKE CLARK SCOTT KING ADAMS JAMES FORD MILLER

11. List the following details for all employees who have a manager.

ename job sal SMITH CLERK £800.00 ALLEN SALESMAN £1,600.00 WARD SALESMAN £1,250.00 JONES MANAGER £2,975.00 MARTIN SALESMAN £1,250.00 BLAKE MANAGER £2,850.00 CLARK MANAGER £2,450.00 SCOTT ANALYST £3,000.00 TURNER SALESMAN £1,500.00 ADAMS CLERK £1,100.00 JAMES CLERK £950.00 FORD ANALYST £3,000.00 MILLER CLERK £1,300.00

9 Exercise 2 Functions

1. Show the remuneration that all the employees receive. Exclude the president:

ename annual_sal SMITH 9600 ALLEN 19500 WARD 15500 JONES 35700 MARTIN 16400 BLAKE 34200 CLARK 29400 SCOTT 60000 TURNER 18000 ADAMS 13200 JAMES 11400 FORD 36000 MILLER 15600

2 Show all the information in the department table in lower case.

Deptno Dept Location 10 accounting new york 20 research dallas 30 sales chicago 40 operations boston

3 Show the name and month of hire of all analysts.

ename monthofhire SCOTT 3 FORD 12

4 Modify your answer to show an annual review to take place a month before the month of the hiredate.

ename reviewdate SCOTT 2 FORD 11

5 How many years has the president worked for the company.

ename years_hired_for KING 10

10 Exercise 3 Group Functions

1. Find the minimum salary of all employees

minimum 800

2. Find the minimum, maximum and average salaries of all employees

min avg max 800 2073.2142857 5000 1429

3. List the minimum and maximum salary for each job type.

job min max ANALYST 3000 3000 CLERK 800 1300 MANAGER 2450 2975 PRESIDENT 5000 5000 SALESMAN 1250 1600

4. Find how many managers have the job title ‘Manager’.

'No of Managers' 3

4a. Find the number of employees reporting directly to each manager. Exclude the president. Note the difference between manager as a job title and the number of employees who are in fact managers.

mgr 'No of Employees' 7566 2 7698 5 7782 1 7788 1 7839 3 7902 1

11 4b Find the different job roles and the number of people employed in each role.

job 'Number of employees' ANALYST 2 CLERK 4 MANAGER 3 PRESIDENT 1 SALESMAN 4

5. Find the average salary and average total remuneration for each job type. Remember salesmen earn commission.

job average monthly remuneration ANALYST 3000 CLERK 1037 MANAGER 2758 PRESIDENT 5000 SALESMAN 1445

6. Find out the difference between highest and lowest salaries

difference 4200

7. Find all departments which have more than 3 employees deptno Employee Count

20 5 30 6

12 If you have time try these:

8. Check whether all employee numbers are indeed unique.

9. List the lowest salaries working for each manager; use manager number rather than job role to create your query. Exclude any groups where the salary is less than 1000. Sort the output by salary.

mgr MinOfsal 7788 1100 7782 1300 7839 2450 7566 3000 5000

13 Exercise 4 Simple Joins

1. Display all employee names and their department names. Order by department.

ename dname MILLER Accounting KING Accounting CLARK Accounting FORD Research ADAMS Research SCOTT Research JONES Research SMITH Research JAMES Sales TURNER Sales BLAKE Sales MARTIN Sales WARD Sales ALLEN Sales

2. Display all employee names, department name where department number is10 order by ename.

ename deptno dname CLARK 10 Accounting KING 10 Accounting MILLER 10 Accounting

3. Display employee names, location and department whose salary is more than £1500 per month. Order by department name.

ename Loc dname KING New York Accounting CLARK New York Accounting FORD Dallas Research SCOTT Dallas Research JONES Dallas Research BLAKE Chicago Sales ALLEN Chicago Sales

14 4. Produce a list showing employees' salary grades.

ename job sal grade JAMES CLERK £950.00 1 ADAMS CLERK £1,100.00 1 SMITH CLERK £800.00 1 MILLER CLERK £1,300.00 2 MARTIN SALESMAN £1,250.00 2 WARD SALESMAN £1,250.00 2 TURNER SALESMAN £1,500.00 3 ALLEN SALESMAN £1,600.00 3 FORD ANALYST £3,000.00 4 SCOTT ANALYST £3,000.00 4 CLARK MANAGER £2,450.00 4 BLAKE MANAGER £2,850.00 4 JONES MANAGER £2,975.00 4 KING PRESIDENT £5,000.00 5

5. Show only employees on grade 3.

ename job sal grade ALLEN SALESMAN £1,600.00 3 TURNER SALESMAN £1,500.00 3

15 Exercise 5 Other join methods

1. Display the department that has no employees.

deptno dname 40 Operations

2. List all employees by name and number along with their manager's details.

e.empno e.ename m.empno m.ename 7902 FORD 7566 JONES 7788 SCOTT 7566 JONES 7900 JAMES 7698 BLAKE 7844 TURNER 7698 BLAKE 7654 MARTIN 7698 BLAKE 7521 WARD 7698 BLAKE 7499 ALLEN 7698 BLAKE 7934 MILLER 7782 CLARK 7876 ADAMS 7788 SCOTT 7782 CLARK 7839 KING 7698 BLAKE 7839 KING 7566 JONES 7839 KING 7369 SMITH 7902 FORD

3. Modify the solution to question 2 to display KING who has no manager.

e.empno e.ename m.empno m.ename 7839 KING 7902 FORD 7566 JONES 7788 SCOTT 7566 JONES 7900 JAMES 7698 BLAKE 7844 TURNER 7698 BLAKE 7654 MARTIN 7698 BLAKE 7521 WARD 7698 BLAKE 7499 ALLEN 7698 BLAKE 7934 MILLER 7782 CLARK 7876 ADAMS 7788 SCOTT 7782 CLARK 7839 KING 7698 BLAKE 7839 KING 7566 JONES 7839 KING 7369 SMITH 7902 FORD

4. Find all jobs that were filled in the first half of 1993 which were also filled during the same period in1995.

job hiredate CLERK 13/6/93 MANAGER 14/5/93 SALESMAN 4/6/95

16 Try these if you have time:

5. Find the employees who joined the company before their manager.

name hiredate manager's name manager's hiredate SMITH 13-Jun-93 FORD 05-Dec-97

6. Show all employees in Dallas.

ename sal loc SMITH £800.00 Dallas JONES £2,975.00 Dallas SCOTT £3,000.00 Dallas ADAMS £1,100.00 Dallas FORD £3,000.00 Dallas

7. List the employee name, job, salary, grade and department name for everyone in the company except clerks. Sort on salary highest first.

ename job sal grade dname KING PRESIDENT £5,000.00 5 Accounting FORD ANALYST £3,000.00 4 Research SCOTT ANALYST £3,000.00 4 Research JONES MANAGER £2,975.00 4 Research BLAKE MANAGER £2,850.00 4 Sales CLARK MANAGER £2,450.00 4 Accounting ALLEN SALESMAN £1,600.00 3 Sales TURNER SALESMAN £1,500.00 3 Sales MARTIN SALESMAN £1,250.00 2 Sales WARD SALESMAN £1,250.00 2 Sales

8. List the following details for employees who earn £36000 a year or are clerks.

ename job annual_sal grade dname KING PRESIDENT 60000 5 Accounting FORD ANALYST 36000 4 Research SCOTT ANALYST 36000 4 Research MILLER CLERK 15600 2 Accounting ADAMS CLERK 13200 1 Research JAMES CLERK 11400 1 Sales SMITH CLERK 9600 1 Research

17 Exercise 6 Running Queries with Parameters

1. Develop a query that will accept a job title from the user.

For example, analysts:

ename job SCOTT ANALYST FORD ANALYST

2. Generate a statement that prompts users at run time. The result should display employees hired between two dates given by the user.

For example, employees hired in the second half of 98 and the first half of 99:

ename hiredate ALLEN 15-Aug-98 MARTIN 05-Dec-98 ADAMS 04-Jun-99

18 Exercise 7 Views

1. Define a view which produces the average, minimum and maximum salary by department:

deptno dname Average Minimum Maximum 10 Accounting 2916 1300 5000 20 Research 2175 800 3000 30 Sales 1566 950 2850

2. Using the view from question 1, extract the following information. Which department has the highest average salary?

Highest average salary 2916

3. Using the view from question1, extract the following information. Employee number or name should be entered at run-time.

deptno ename job sal Average Maximum 10 CLARK MANAGER 2450 2916 5000

19 Exercise 8 Advanced Query Techniques

This exercise will allow you to write queries using nested selects and correlated selects.

1. Find the employee(s) who earn the highest salary in each job type.

job ename sal PRESIDENT KING £5,000.00 ANALYST FORD £3,000.00 ANALYST SCOTT £3,000.00 MANAGER JONES £2,975.00 SALESMAN ALLEN £1,600.00 CLERK MILLER £1,300.00

2. Find the employees who earn the minimum salary for their job. Display the result in ascending order of salary.

ename job sal SMITH CLERK £800.00 MARTIN SALESMAN £1,250.00 WARD SALESMAN £1,250.00 CLARK MANAGER £2,450.00 FORD ANALYST £3,000.00 SCOTT ANALYST £3,000.00 KING PRESIDENT £5,000.00

3. Show the following details for any employee who earns a salary greater than the average for their department.

ename sal deptno KING £5,000.00 10 JONES £2,975.00 20 FORD £3,000.00 20 SCOTT £3,000.00 20 ALLEN £1,600.00 30 BLAKE £2,850.00 30

4. Who works for the Sales department but is not a salesman?

ename job sal BLAKE MANAGER £2,850.00 JAMES CLERK £950.00

20 Appendix 1 Setting up the Employee database

If you do not have access to a copy of the employee database, set up the tables with the following fieldnames and field descriptions in Access and add the data

Dept

Fieldnames Data Types Field Size Decimal Primary Key - Places Indexed Yes(No Duplicates) Deptno Integer Double Auto Yes Dname Text 50 Loc Text 50

Salgrade

Field Data Field Format Decimal Primary Key - names Types Size Places Indexed Yes(No Duplicates) Grade Number Single Auto Yes Losal Number Integer £#,##0.00;- Auto £#,##0.00 Hisal Number Integer £#,##0.00;- Auto £#,##0.00

Emp

Field Data Field Format Decimal Primary Key - names Types Size Places Indexed Yes(No Duplicates) Empno Number Integer Auto Yes Ename Text 50 Job Text 50 Mgr Number Integer Auto Hiredate Date/Time Medium Date Sal Number Integer £#,##0.00;- Auto £#,##0.00 Comm Number Integer £#,##0.00;- Auto £#,##0.00 Deptno Number Double Auto

21 The Employee database

Dept

deptno dname Loc 10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston

Salgrade

grade losal hisal 1 £700.00 £1,200.00 2 £1,201.00 £1,400.00 3 £1,401.00 £2,000.00 4 £2,001.00 £3,000.00 5 £3,001.00 £9,999.00

Emp

empno ename job mgr hiredate sal comm deptno 7369 SMITH CLERK 7902 13-Jun-93 £800.00 £0.00 20 7499 ALLEN SALESMAN 7698 15-Aug-98 £1,600.00 £300.00 30 7521 WARD SALESMAN 7698 26-Mar-96 £1,250.00 £500.00 30 7566 JONES MANAGER 7839 31-Oct-95 £2,975.00 20 7654 MARTIN SALESMAN 7698 05-Dec-98 £1,250.00 £1,400.00 30 7698 BLAKE MANAGER 7839 11-Jun-92 £2,850.00 30 7782 CLARK MANAGER 7839 14-May-93 £2,450.00 10 7788 SCOTT ANALYST 7566 05-Mar-96 £3,000.00 20 7839 KING PRESIDENT 09-Jun-90 £5,000.00 10 7844 TURNER SALESMAN 7698 04-Jun-95 £1,500.00 £0.00 30 7876 ADAMS CLERK 7788 04-Jun-99 £1,100.00 20 7900 JAMES CLERK 7698 23-Jun-00 £950.00 30 7902 FORD ANALYST 7566 05-Dec-97 £3,000.00 20 7934 MILLER CLERK 7782 21-Jan-00 £1,300.00 10

NB: Salary is a monthly record and commission is a yearly record.

22 23

Recommended publications