Oracle

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

Oracle Database

>collection of data treated as a unit


>purpose of a database is to store and retrieve related information
>database server is the key to solving the problems of information management
>a server reliably manages a large amount of data in a multiuser environment so
that many users can concurrently access the same data
>database server also prevents unauthorized access and provides efficient solutions
for failure recovery

Oracle Basic
1.Write a query to list first name, last name and their salary for employee
contained in the employees table.

= SELECT first_name, last_name, salary


from employees;

2.Write a query to display all the columns of 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.

= SELECT first_name FirstName, last_name LastName, salary Salary


FROM employees;

4.Write a query to list first name, last name and their salary for first 10
employee contained in the employees table.

= SELECT first_name, last_name, salary


FROM employees
WHERE ROWNUM < 11;

Oracle Operators
1.Write a query to list the name of all the employees who are working in department
number 20.

= SELECT first_name, last_name, department_id


FROM employees
WHERE department_id =20;

2.Write a query to list the employees name and total salary of a year and yearly
salary is more than $10000.

= SELECT first_name, last_name, salary*12 "Yearly Salary"


FROM employees
WHERE (salary*12) >10000;

3.Write a query to list the employees name and salary who�s daily salary is more
than $100.

= SELECT first_name, last_name, salary


FROM employees
WHERE (salary/30) >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.

= SELECT first_name, salary


FROM employees
WHERE job_id='AC_MGR' and salary>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.

= SELECT first_name, salary


FROM employees
WHERE job_id='FI_ACCOUNT' and salary < 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.

= SELECT first_name, salary


FROM employees
WHERE job_id='FI_ACCOUNT' and (salary>=5000 and salary<=20000);

8.Write a query to list the names, salary of all the employees who are working with
a commission package.

= SELECT EMPLOYEE_ID,COMMISSION_PCT ,SALARY


FROM employees
WHERE commission_pct IS NOT NULL;

9.Write a query to list the name, salary of all the employees where employee first
name belongs in a specified list.

= SELECT first_name, salary


FROM employees
WHERE first_name IN ('Peter', 'Smith');

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.

= SELECT first_name, last_name, job_id


FROM employees
WHERE job_id NOT IN ('IT_PROG', 'FI_ACCOUNT')
ORDER BY salary;

11.Write a query to list the name (first and last name), hire date of all the
employees who joined before or after 2005.

= SELECT first_name, last_name, hire_date


FROM employees
WHERE to_char (hire_date, 'YYYY') NOT IN ('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

= SELECT CONCAT (CONCAT (CONCAT(CONCAT(first_name,' '),last_name), ' is a


'),job_id)
FROM employees
WHERE salary > 2000;

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�;

Oracle Wildcard special Operators


1.Write a query to list the names (first and last) of those employees whose name
starts with A.

= SELECT first_name, last_name


FROM employees
WHERE first_name LIKE 'A%';

2.Write a query to list the names (first and last) of those employees whose last
name ends with a.

= SELECT first_name, last_name


FROM employees
WHERE last_name LIKE '%a';

3.Write a query to list the names (first and last) of those employees whose name
have second alphabet 's' in their names.

= SELECT first_name, last_name


FROM employees
WHERE first_name LIKE '_s%';

4.Write a query to list the names (first and last) of those employees whose first
name has only five characters.

= SELECT first_name, last_name


FROM employees
WHERE length(first_name) = 5;
5.Write a query to list the names (first and last) of those employees whose first
name has only five characters and starts with �A�.

= SELECT first_name, last_name


FROM employees
WHERE first_name like �A%� and length(first_name) = 5;

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'.

= SELECT first_name, last_name


FROM employees
WHERE length(first_name) = 5 and last_name like �__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'.

= SELECT first_name, last_name


FROM employees
WHERE length(first_name) = 5 and first_name like �S%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'.

= SELECT first_name, last_name, hire_date


FROM employees
WHERE to_char(hire_date, �MON �) LIKE �_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.

= SELECT first_name, last_name, salary


FROM employees
WHERE length (salary) = 4 and salary like �%0�;

10.Write a query to list the names (first and last), salary of those employees
whose names having a character set �ll� together.

= SELECT first_name, last_name, salary


FROM employees
where first_name LIKE �%ll%�;

11.Write a query to list first_name, last_name of employees with the pattern 'l_x'
in their first name.

= SELECT first_name, last_name


FROM employees
WHERE first_name LIKE '%l_x%'
ORDER BY 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;

*Cross Join / Cartesian Products


>A Cross Join or Cartesian join or Cartesian product is a join of every row of one
table to every row of another table.

= 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.

~Left Outer Join


>This left outer join displays all matching records of both table along with
the records in left hand side table of join clause which are not in right hand side
table of join clause.

= 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(+);

~Right Outer Join


>This right outer join displays all matching records of both tables along
with the records in left hand side table of join clause which are not in right hand
side table of join clause.

= 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;

~Full Outer Join


>A full outer join returns all rows from both the tables left and right side
of the join clause, extended with nulls if they do not satisfy the join condition.

= 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

= SELECT * FROM emp_mast


WHERE dept_no NOT IN (
SELECT dept_no FROM dep_mast);

*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.

= SELECT * FROM dep_mast a


WHERE EXISTS
(SELECT * FROM emp_mast b
WHERE a.dept_no = b.dept_no);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy