Oracle
Oracle
Oracle
Oracle Basic
1.Write a query to list first name, last name and their salary for employee
contained in the employees table.
= SELECT *
FROM employee;
3.Write a query to display first name, last name and their salary of employees
where column headings will be specified as aliases: FirstName, LastName and Salary.
4.Write a query to list first name, last name and their salary for first 10
employee contained in the employees table.
Oracle Operators
1.Write a query to list the name of all the employees who are working in department
number 20.
2.Write a query to list the employees name and total salary of a year and yearly
salary is more than $10000.
3.Write a query to list the employees name and salary who�s daily salary is more
than $100.
4.Write a query to list the name of all the employees who are not working in
department number 20.
= SELECT first_name, last_name, department_id
FROM employees
WHERE department_id !=20;
5.Write a query to list the name of all the employees who are working as account
manager and drawing a salary more than $5000.
6.Write a query to list the names of all the employees who are working as
accountant in finance section and drawing a salary less than $20000.
7.Write a query to list the name of all the employees who are working as accountant
in finance section and drawing a salary is greater than equal to $5000 and less
than equal to $20000.
8.Write a query to list the names, salary of all the employees who are working with
a commission package.
9.Write a query to list the name, salary of all the employees where employee first
name belongs in a specified list.
10.Write a query to list the first name, last name, Job id of all the employees
except "IT_PROG " & " FI_ACCOUNT" in asc order of Salaries.
11.Write a query to list the name (first and last name), hire date of all the
employees who joined before or after 2005.
12.Write query to list the name (first and last name), hire date of all the
employees who joined on 1-JUL-2006, 24-MAR-2007, 04-JAN-2008 in ascending order of
seniority.
= SELECT first_name, last_name, hire_date
FROM employees
WHERE hire_date IN('01-JUL-2006','24-MAR-2007','04-JAN-2008')
ORDER BY hire_date asc;
13. Write a query to concatenate first name, last name and job id from employees
table in the following format.
Sample Format :
Douglas Grant is a SH_CLERK
Jennifer Whalen is a AD_ASST
Michael Hartstein is a MK_MAN
Pat Fay is a MK_REP
Susan Mavris is a HR_REP
Hermann Baer is a PR_REP
Shelley Higgins is a AC_MGR
William Gietz is a AC_ACCOUNT
14.Write a query to list the employees who are joined in the year between '01-Jan-
2004' and '31-Dec-2008'.
= SELECT *
FROM employees
WHERE HIRE_DATE between �01-jan-2004� and �31-dec-2008�;
2.Write a query to list the names (first and last) of those employees whose last
name ends with a.
3.Write a query to list the names (first and last) of those employees whose name
have second alphabet 's' in their names.
4.Write a query to list the names (first and last) of those employees whose first
name has only five characters.
6.Write a query to list the names (first and last) of those employees whose first
name has only five characters and last name have third alphabet ends with 's'.
7.Write a query to list the names (first and last) of the employees whose first
name has only five characters and starting with 'S' and ending with 'n'.
8.Write a query to list the names (first and last), hire date of those employees
who joined in the month of which second character is 'u'.
9.Write a query to list the names (first and last), salary of those employees whose
salary is four digit number ending with Zero.
10.Write a query to list the names (first and last), salary of those employees
whose names having a character set �ll� together.
11.Write a query to list first_name, last_name of employees with the pattern 'l_x'
in their first name.
Oracle Joins
>purpose of a join is to combine the data from two or more tables, views, or
materialized views.
>a join is actually performed whenever multiple tables appear in the FROM clause of
the query and by the where clause which combines the specified rows of tables
>a join involves in more than two tables then Oracle joins first two tables based
on the joins condition and then compares the result with the next table and so on
Join Conditions
>A join queries must have contained at least one join condition, either in the FROM
clause or in the WHERE clause. The join condition compares two columns from two
different tables. The Oracle Database combines pairs of rows, from each table,
participating in joining, which are satisfying the join condition evaluates to
TRUE.
>A WHERE clause that contains a join condition can also contain other conditions
that refer to columns of only one table. These conditions can further restrict the
rows returned by the join query.
Types of JOIN
*Equijoins
>An equijoin is a join with a join condition containing an equality operator. This
is represented by (=) sign. This join retrieves information by using equality
condition.
= SELECT emp_no,emp_name,job_name,dep_name
FROM emp_mast e,dep_mast d
WHERE e.dept_no=d.dept_no;
*Non-Equi Joins
>An nonequi join is an inner join statement that uses an unequal operation (i.e.:
<>, >, <, !=, BETWEEN, etc.) to match rows from different tables.
= SELECT emp_no,emp_name,job_name,dep_name
FROM emp_mast e,dep_mast d
WHERE e.dept_no>d.dept_no;
*Self Joins
>A self join is such a join in which a table is joined with itself. For example,
when you require details about an employee and his manager (also an employee).
= SELECT a1.emp_no,a2.emp_name,a1.job_name,a2.dept_no
FROM emp_mast a1,emp_mast a2
WHERE a1.emp_no=a2.mgr_id;
= SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mast
CROSS JOIN dep_mast;
*Inner Joins
>An inner join is a join that returns rows of the tables that satisfy the join
condition.
= SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mast
INNER JOIN dep_mast USING(dept_no);
*Outer Joins
>An outer join is such a join which is similar to the equi join, but Oracle will
also return non matched rows from the table.
= SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mast e LEFT OUTER JOIN dep_mast d
ON(e.dept_no=d.dept_no);
OR
= SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mast e, dep_mast d
WHERE e.dept_no=d.dept_no(+);
= SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mast e RIGHT OUTER JOIN dep_mast d
ON(e.dept_no=d.dept_no);
OR
= SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mast e, dep_mast d
WHERE e.dept_no(+)=d.dept_no;
= SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mast e
FULL OUTER JOIN dep_mast d ON(e.dept_no=d.dept_no);
*Natural Join
>A natural join is such a join that compares the common columns of both tables with
each other.
= SELECT emp_no,emp_name,job_name,dep_name,location
FROM emp_mast
NATURAL JOIN dep_mast;
*Antijoins
>An antijoin between two tables returns rows from the first table where no matches
are found in the second table. Anti-Joins are only available when performing a NOT
IN sub-query
*Semijoins
>A semi-join is such a join where the EXISTS clause is used with a subquery. It can
be called a semi-join because even if duplicate rows are returned in the subquery,
only one set of matching values in the outer query is returned.