السلام عليكم ورحمه الله وبركاته
حقدم لكم مجموعه من الاستعلامات الذي كنا نتمرن عليها ايام الدراسه واتمني تعجبكم
Chapter 1
Practice 1
1. Initiate an iSQL*Plus session using the user ID and password provided by the instructor.
User_id : scott
Password: tiger
2. iSQL*Plus commands access the database.
True/False
True
3. The following select statement executes successfully:
select last_name, job_id, salary AS Sal FROM employees;
True/False
True
4. The following select statement executes successfully:
select *
FROM job_grades;
True/False
False
5. There are four coding errors in this statement. Can you identify them?
select employee_id, last_name
sal x 12 ANNUAL SALARY
FROM employees;
False
6. Show the structure of the DEPARTMENTS table. select all data from the table.
1.desc departments;
2. select *
FROM departments;
7. Show the structure of the EMPLOYEES table. create a query to display the last name, job code, hire date, and employee number for each employee, with employee number appearing first. Provide an alias STARTDATE for the HIRE_DATE column.
desc employees;
8. Run your query in the file lab1_7.sql.
select employee_id, last_name, job_id, hire_date "Start Date"
FROM employees;
9. create a query to display unique job codes from the EMPLOYEES table.
select job_id
FROM employees;
10. Copy the statement from lab1_7.sql into the iSQL*Plus Edit window. Name the column headings Emp #, Employee, Job, and Hire Date, respectively.
select employee_id "emp#", last_name "Employee" ,job_id "Job",
hire_date "hire date"
FROM employees;
11. Display the last name concatenated with the job ID, separated by a comma and space, and name the column Employee and Title.
select last_name || ',' || job_id "employee and title"
FROM employees;
12. create a query to display all the data from the EMPLOYEES table. Separate each column by a comma. Name the column THE_OUTPUT
select EMPLOYEE_ID||','
||FIRST_NAME||','||LAST_NAME||','||EMAIL||','
||PHONE_NUMBER||','||HIRE_DATE||','||JOB_ID||','
||SALARY||','||COMMISSION_PCT|| ','
||MANAGER_ID||','||DEPARTMENT_ID "THE _OUTPUT"
FROM employees;
Chapter 2
Practice 2
1. create a query to display the last name and salary of employees earning more than $12,000.
select last_name ,salary
FROM employees
WHERE salary >12000;
2. create a query to display the employee last name and department number for employee number
176
select last_name,department_id
FROM employees
WHERE employee_id=176;
3. Modify lab2_1.sql to display the last name and salary for all employees whose salary is not in the range of $5,000 and $12,000.
select last_name,salary
FROM employees
WHERE salary not between 12000 and 55000;
4. Display the employee last name, job ID, and start date of employees hired between February 20, 1998, AND May 1, 1998. Order the query in ascending order by start date.
select last_name,job_id ,hire_date
FROM employees
WHERE hire_date between '20-feb-98' AND '01-may-98';
5. Display the last name and department number of all employees in departments 20 AND 50 in alphabetical order by name.
select last_name ,department_id
FROM employees
WHERE department_id in(20,50)
ORDER BY last_name;
6. Modify lab2_3.sql to list the last name and salary of employees who earn between $5,000 and $12,000, and are in department 20 or 50. Label the columns Employee and Monthly Salary, respectively.
select last_name "employee",salary "monthly salary"
FROM employees
WHERE salary between 12000 and 5000
AND department_id in(20,50);
7. Display the last name and hire date of every employee who was hired in 1994.
select last_name , hire_date
FROM employees
WHERE hire_date like'%94';
8. Display the last name and job title of all employees who do not have a manager.
select last_name ,job_id
FROM employees
WHERE manager_id is null;
9. Display the last name, salary, and commission for all employees who earn commissions. Sort data in descending Order of salary and commissions
select last_name , salary, commission_pct
FROM employees
WHERE commission_pct is not null
ORDER BY salary DESC;
10. Display the last names of all employees where the third letter of the name is an a.
select last_name
FROM employees
WHERE last_name like'__a%';
11. Display the last name of all employees who have an a and an e in their last name.
select last_name
FROM employees
WHERE last_name like'%a%'
OR last_name like '%e%';
12. Display the last name, job, and salary for all employees whose job is sales representative or stock clerk and whose salary is not equal to $2,500, $3,500, or $7,000.
select last_name,job_id,salary
FROM employees
WHERE job_id like'ST_CLERK'
OR job_id like 'SA_REP'
AND salary not in(2005,3500,7000);
13. Modify lab2_6.sql to display the last name, salary, and commission for all employees whose commission amount is 20%.
select last_name "Employee",salary "Monthly Salary",commission_pct
FROM employees
WHERE commission_pct =.2
Chapter 3
Practice 3
1.Write a query to display the current date. Label the column Date.
select sysdate “data”
FROM employees;
2. FOR each employee, display the employee number, last_name, salary, AND salary increased by 15% and expressed as a whole number. Label the column New Salary.
select EMPLOYEE_ID , LAST_NAME , salary,salary*1.15
"NEW _SALARY"
FROM employees;
3. Run your query in the file lab3_2.sql.
4. Modify your query lab3_2.sql to add a column that subtracts the old salary from the new salary. Label the column Increase.
select EMPLOYEE_ID,LAST_NAME, salary,
salary*1.15 "NEW _ SALARY",salary*1.15- salary "Increase"
FROM employees;
5. Write a query that displays the employee’s last names with the first letter capitalized and all other letters lowercase, and the length of the names, for all employees whose name starts with J, A, or M. Give each column an appropriate label. Sort the results by the employees’ last_names.
select initcap (last_name) "name",length(last_name) "length"
FROM employees
WHERE last_name like 'A%'
OR last_name like 'J%'
OR last_name like 'M%'
ORDER BY last_name;
6. for each employee, display the employee’s last name, and calculate the number of months between today and the date the employee was hired. Label the column MONTHS_WORKED.
order your results by the number of months employed. Round the number of months up to the closest whole number.
select last_name ,
round (months_between(sysdate,
hire_date),0)"MONTHS_WORKE"
FROM employees
ORDER BY round (months_between( sysdate ,hire_date),0);
7. Write a query that produces the following for each employee:
<employee last name> earns <salary> monthly but wants <3 times
salary>. Label the column Dream Salaries.
select last_name ||'earns' || to_char(salary,'$99,999.00') ||'monthly but
wents'||to_char(3* salary,'$99,999.00') "dream salaries"
FROM employees;
8. create a query to display the last name and salary for all employees. format the salary to be characters long, left-padded with $. Label the column SALARY.
select last_name, lpad (salary,15,'$') "salary"
FROM employees;
9. Display each employee’s last name, hire date, and salary review date, which is the first Monday after six months of service. Label the column REVIEW. format the dates to appear in the
format similar to “Monday, the Thirty-First of July, 2000.”
select last_name ,hire_date,
to_char(next_day(add_months(hire_date,6) ,'monday'),
'day "the" fmDdspth"of" month yyyy') "review"
FROM employees;
10. Display the last name, hire date, and day of the week on which the employee started. Label the column DAY. order the results by the day of the week starting with Monday.
select last_name ,hire_date,to_char(hire_date,'day') "day"
FROM employees
ORDER BY to_char(hire_date,'day') ;
11. create a query that displays the employees’ last names and commission amounts. If an employee does not earn commission, put “No Commission.” Label the column COMM.
select last_name ,nvl (to_char(commission_pct),'NO COMMISSION')
FROM employees;
12. create a query that displays the employees’ last names and indicates the amounts of their annual salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
select rpad(last_name,trunc(salary/1000,0)+length(last_name),'*')
EMPLOYEES_AND_THEIR_SALARIES
FROM employees;;
13. Using the DECODE function, write a query that displays the grade of all employees based on the value of the column JOB_ID, as per the following data:
Job Grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
select job_id ,
Decode (job_id, 'AD_PRES','A',
'ST_MAN','B',
'IT_PROG','C',
'SA_REP','D',
'ST_CLERK','E',
'0')
FROM employees;
Chapter 4
Practice 4
1. Write a query to display the last name, department number, and department name for all employees.
select e.last_name, e.department_id ,d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id;
2. create a unique listing of all jobs that are in department 80. Include the location of the department in the output.
select e.job_id , d.location_id
FROM employees e, departments d
WHERE d.department_id= 80
AND e.department_id = d.department_id;
3. Write a query to display the employee last name, department name, location ID, and city of all employees who earn a commission.
select e.last_name,d.department_name,l.location_id,l.city
FROM employees e, departments d ,locations l
WHERE e.department_id = d.department_id
AND d.location_id=l.location_id
AND commission_pct is not null ;
4. Display the employee last name and department name for all employees who have an a (lowercase) in their last names.
select e.last_name,d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id
AND last_name like '%a%' ;
5. Write a query to display the last name, job, department number, and department name for all employees who work in Toronto.
select e.last_name ,e.job_id,e.department_id,d.department_name
FROM employees e,departments d,locations l
WHERE l.city like 'Toronto'
AND e.department_id=d.department_id;
Chapter 5
Practice 5
Determine the validity of the following three statements. Circle either True or False.
1. Group functions work across many rows to produce one result per group.
True/False
True
2. Group functions include nulls in calculations.
True/False
False
3. The WHERE clause restricts rows prior to inclusion in a group calculation.
True/False
False
4. Display the highest, lowest, sum, and average salary of all employees. Label the columns Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest whole number.
select max(salary)"Maximum",min(salary)"Minimum",
sum (salary)"Sum", avg(salary)"Average"
FROM employees;
5. Modify the query in lab5_4.sql to display the minimum, maximum, sum, and average salary for each job type.
select job_id,max(salary)"Maximum",min(salary)"Minimum",
sum (salary)"Sum", avg(salary)"Average"
FROM employees
GROUP BY job_id;
6. Write a query to display the number of people with the same job.
select job_id,count(employee_id)
FROM employees
GROUP BY job_id;;
7. Determine the number of managers without listing them. Label the column Number of Managers. Hint: Use the MANAGER_ID column to determine the number of managers.
select count (DISTINCT(manager_id)) " Number of Manager"
FROM employees
8. Write a query that displays the difference between the highest and
lowest salaries. Label the column DIFFERENCE.
select max(salary)-min(salary) difference
FROM employees;;
9. Display the manager number and the salary of the lowest paid employee for that manager. Exclude anyone whose manager is not known. Exclude any groups WHERE the minimum salary is $6,000 or less. Sort the output in descending Order of salary.
select manager_id ,min(salary)
FROM employees
HAVING min(salary)>=6000
AND manager_id is not null
GROUP BY manager_id
ORDER BY min(salary) DESC;
10. Write a query to display each department’s name, location, number of employees, and the average salary for all employees in that department. Label the columns Name, Location, Number of People, and Salary, respectively. Round the average salary to two decimal places.
select d.department_name "Name",d.location_id "Location",
count(e.employee_id) "Nember of People",avg(e.salary)
"Salary"
FROM departments d,employees e
WHERE d.department_id= e.department_id
GROUP BY d.department_name,location_id ;
11. create a query that will display the total number of employees and, of that total, the number of employees hired in 1995, 1996, 1997, and 1998. create appropriate column headings.
select count(employee_id) "Total",
sum(decode(to_char(hire_date,'yyyy'),1995,1,0))"1995",
sum(decode(to_char(hire_date,'yyyy'),1996,1,0))"1996",
sum(decode(to_char(hire_date,'yyyy'),1997,1,0))"1997",
sum(decode(to_char(hire_date,'yyyy'),1998,1,0))"1998"
FROM employees;
12. create a matrix query to display the job, the salary for that job based on department number, and the total salary for that job, for departments 20, 50, 80, and 90, giving each column an appropriate heading.
select job_id "Job",
sum(decode (department_id,20,salary,0))"dept 20",
sum(decode (department_id,50,salary,0))"dept 50",
sum(decode (department_id,80,salary,0))"dept 80",
sum(decode (department_id,90,salary,0))"dept 90",
sum(salary)
FROM employees
GROUP BY job_id;
حررت من قبل:
وليد القدسي في
الأحد,16/فر/1431 هـ,09:42 مساءً