Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Practice 6

1. Write a query to display the last name and hire date of any employee in the same
department as Zlotkey. Exclude Zlotkey.

SELECT last_name, hire_date


FROM employees
WHERE department_id = (SELECT department_id
FROM employees
WHERE last_name = 'Zlotkey')
AND last_nae <> 'Zlotkey';
2. Create a query to display the employee numbers and last names of all employees who earn more than the
average salary. Sort the results in ascending order of salary.

SELECT employee_id, last_name


FROM employees
WHERE salary > (SELECT AVG(salary)
FROM employees);

employee_id last_name

100 King

101 Kochhar

Introduction to Oracle9i: SQL 6-20


102 De Haan

103 Hunold

108 Greenberg

109 Faviet

110 Chen

111 Sciarra

112 Urman

113 Popp

114 Raphaely

120 Weiss

121 Fripp

122 Kaufling

123 Vollman

145 Russell

146 Partners

Introduction to Oracle9i: SQL 6-21


147 Errazuriz

148 Cambrault

149 Zlotkey

150 Tucker

151 Bernstein

152 Hall

153 Olsen

154 Cambrault

155 Tuvault

156 King

157 Sully

158 McEwen

159 Smith

160 Doran

161 Sewall

Introduction to Oracle9i: SQL 6-22


162 Vishney

163 Greene

164 Marvins

165 Lee

168 Ozer

169 Bloom

170 Fox

171 Smith

172 Bates

174 Abel

175 Hutton

176 Taylor

177 Livingston

178 Grant

201 Hartstein

Introduction to Oracle9i: SQL 6-23


203 Mavris

204 Baer

205 Higgins

206 Gietz

3. Write a query that displays the employee numbers and last names of all employees who work in a
department with any employee whose last name contains a u. Place your SQL statement in a text file
named lab6_3.sql. Run your query.

SELECT employee_id, last_name


FROM employees
WHERE department_id IN (SELECT department_id
FROM employees
WHERE last_name like '%u%');

employee_id last_name

103 Hunold

104 Ernst

Introduction to Oracle9i: SQL 6-24


105 Austin

106 Pataballa

107 Lorentz

114 Raphaely

115 Khoo

116 Baida

117 Tobias

118 Himuro

119 Colmenares

120 Weiss

121 Fripp

122 Kaufling

123 Vollman

124 Mourgos

125 Nayer

Introduction to Oracle9i: SQL 6-25


126 Mikkilineni

127 Landry

128 Markle

129 Bissot

130 Atkinson

131 Marlow

132 Olson

133 Mallin

134 Rogers

135 Gee

136 Philtanker

137 Ladwig

138 Stiles

139 Seo

140 Patel

Introduction to Oracle9i: SQL 6-26


141 Rajs

142 Davies

143 Matos

144 Vargas

145 Russell

146 Partners

147 Errazuriz

148 Cambrault

149 Zlotkey

150 Tucker

151 Bernstein

152 Hall

153 Olsen

154 Cambrault

155 Tuvault

Introduction to Oracle9i: SQL 6-27


156 King

157 Sully

158 McEwen

159 Smith

160 Doran

161 Sewall

162 Vishney

163 Greene

164 Marvins

165 Lee

166 Ande

167 Banda

168 Ozer

169 Bloom

170 Fox

Introduction to Oracle9i: SQL 6-28


171 Smith

172 Bates

173 Kumar

174 Abel

175 Hutton

176 Taylor

177 Livingston

179 Johnson

180 Taylor

181 Fleaur

182 Sullivan

183 Geoni

184 Sarchand

185 Bull

186 Dellinger

Introduction to Oracle9i: SQL 6-29


187 Cabrio

188 Chung

189 Dilly

190 Gates

191 Perkins

192 Bell

193 Everett

194 McCain

195 Jones

196 Walsh

197 Feeney

198 OConnell

199 Grant

Introduction to Oracle9i: SQL 6-30


Practice 6 (continued)

4. Display the last name, department number, and job ID of all employees whose
department location ID is 1700.

SELECT last_name, department_id, job_id


FROM employees
WHERE department_id IN (SELECT department_id
FROM departments
WHERE location_id = 1700);

last_name department_id job_id

King 90 AD_PRES

Kochhar 90 AD_VP

De Haan 90 AD_VP

Greenberg 100 FI_MGR

Faviet 100 FI_ACCOUNT

Chen 100 FI_ACCOUNT

Sciarra 100 FI_ACCOUNT

Introduction to Oracle9i: SQL 6-31


Urman 100 FI_ACCOUNT

Popp 100 FI_ACCOUNT

Raphaely 30 PU_MAN

Khoo 30 PU_CLERK

Baida 30 PU_CLERK

Tobias 30 PU_CLERK

Himuro 30 PU_CLERK

Colmenares 30 PU_CLERK

Whalen 10 AD_ASST

Higgins 110 AC_MGR

Gietz 110 AC_ACCOUNT

5. Display the last name and salary of every employee who reports to King.

SELECT last_name, salary

FROM employees
Introduction to Oracle9i: SQL 6-32
WHERE manager_id = (SELECT employee_id

FROM employees

WHERE last_name = 'King');

6. Display the department number, last name, and job ID for every employee in the Executive
department.

If you have time, complete the following exercises:


SELECT department_id, last_name, job_id
FROM employees
WHERE department_id IN (SELECT department_id
FROM departments
WHERE department_name = 'Executive');

department_id last_name job_id

90 King AD_PRES

90 Kochhar AD_VP

90 De Haan AD_VP

7. Modify the query in lab6_3.sql to display the employee numbers, last names, and salaries of all
employees who earn more than the average salary and who work in a department with any
employee with a u in their name. Resave lab6_3.sql to lab6_7.sql. Run the statement in
lab6_7.sql.

SELECT employee_id, last_name, salary


FROM employees
WHERE department_id IN (SELECT department_id
FROM employees
WHERE last_name like '%u%')
AND salary > (SELECT AVG(salary)
Introduction to Oracle9i: SQL 6-33
FROM employees);

employee_id last_name salary

103 Hunold 9000.00

114 Raphaely 11000.00

120 Weiss 8000.00

121 Fripp 8200.00

122 Kaufling 7900.00

123 Vollman 6500.00

145 Russell 14000.00

146 Partners 13500.00

147 Errazuriz 12000.00

148 Cambrault 11000.00

149 Zlotkey 10500.00

150 Tucker 10000.00

151 Bernstein 9500.00

Introduction to Oracle9i: SQL 6-34


152 Hall 9000.00

153 Olsen 8000.00

154 Cambrault 7500.00

155 Tuvault 7000.00

156 King 10000.00

157 Sully 9500.00

158 McEwen 9000.00

159 Smith 8000.00

160 Doran 7500.00

161 Sewall 7000.00

162 Vishney 10500.00

163 Greene 9500.00

164 Marvins 7200.00

165 Lee 6800.00

168 Ozer 11500.00

Introduction to Oracle9i: SQL 6-35


169 Bloom 10000.00

170 Fox 9600.00

171 Smith 7400.00

172 Bates 7300.00

174 Abel 11000.00

175 Hutton 8800.00

176 Taylor 8600.00

177 Livingston 8400.00

Introduction to Oracle9i: SQL 6-36

You might also like