Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.

Similar presentations


Presentation on theme: "SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City."— Presentation transcript:

1 SQL/PL SQL Oracle By Rana Umer

2 Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City with Person ID Primary Key Q2. Insert a record in the "Persons Table" from another table PersonBackup same structure as Person Table. Q3. Update the record City =Islamabad where City is Lahore. Q4. Show all record in the Person Table where City is Islamabad Q5. Show Person ID, FirstName & LastName from Person where City is Islamabad. Q6. Create View to show Person Table. Q7. Show record from Person View Q8. Add new column ContactNo number in Person Table Q9. Create User Ali assign role DBA. Q10. Create User Khalid assign Select, insert privileges Q11. Remove insert privileges from Khalid. Q12. Create Index on Person ID. Q13. Show ROWID of the Indexes. Oracle By Rana Umer

3 Displaying Data from Multiple Tables Oracle By Rana Umer Types of Joins Joins that are compliant with the SQL:1999 standard include the following: – Natural joins: NATURAL JOIN clause USING clause ON clause – Outer joins: LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN – Cross joins

4 Displaying Data from Multiple Tables Oracle By Rana Umer An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. The most common type of join is: SQL equijoins (INNER JOIN) (simple join). An SQL INNER JOIN return all rows from multiple tables where the join condition is met. List the types the different SQL JOINs you can use: INNER JOIN: Returns all rows when there is at least one match in BOTH tables LEFT JOIN: Return all rows from the left table, and the matched rows from the right table RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table FULL JOIN: Return all rows when there is a match in ONE of the tables Natural JOIN: Return all rows of common columns in both the tables

5 Displaying Data from Multiple Tables Oracle By Rana Umer EMPLOYEES DEPARTMENTS … …

6 Displaying Data from Multiple Tables Oracle By Rana Umer SELECTtable1.column, table2.column FROMtable1 [NATURAL JOIN table2] Or [JOIN table2 USING (column_name)] Or [JOIN table2 ON (table1.column_name = table2.column_name)] Natural joins:

7 Displaying Data from Multiple Tables Oracle By Rana Umer SELECT employee_id, last_name, location_id, department_id FROM employees, departments Where employees. department_id =Departments.department_id; SELECT employee_id, last_name, location_id, department_id FROM departments JOIN employees ON employees. department_id =Departments.department_id; SELECT employee_id, last_name, location_id, department_id FROM employees JOIN departments USING (department_id) ; SELECT employee_id, last_name, location_id, department_id FROM employees Natural JOIN departments; Natural joins:

8 Displaying Data from Multiple Tables Oracle By Rana Umer Table Alias SELECT e.employee_id, e.last_name, e.location_id, d.department_id FROM employees e, departments d Where e.department_id =d.department_id; SELECT e.employee_id, e.last_name, e.location_id, department_id FROM employees e JOIN departments d USING (department_id) ;

9 Displaying Data from Multiple Tables Oracle By Rana Umer SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON (e.department_id = d.department_id) AND e.manager_id = 149 ; SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON (e.department_id = d.department_id) WHERE e.manager_id = 149 ; Use the AND clause or the WHERE clause to apply additional conditions: Additional Conditions to a Join

10 Displaying Data from Multiple Tables Oracle By Rana Umer Self joins: Joining a Table to Itself MANAGER_ID in the WORKER table is equal to EMPLOYEE_ID in the MANAGER table. EMPLOYEES (WORKER)EMPLOYEES (MANAGER) …… EMPLOYEES (WORKER)EMPLOYEES (MANAGER)

11 Displaying Data from Multiple Tables Oracle By Rana Umer Self joins: Using the ON Clause … SELECT worker.last_name emp, manager.last_name mgr FROM employees worker JOIN employees manager ON (worker.manager_id = manager.employee_id);

12 Displaying Data from Multiple Tables Oracle By Rana Umer NonEqui joins: EMPLOYEES JOB_GRADES … JOB_GRADES table defines the LOWEST_SAL and HIGHEST_SAL range of values for each GRADE_LEVEL. Hence, the GRADE_LEVEL column can be used to assign grades to each employee. On NonEqui Join Condition does with out ‘=‘ sign

13 Displaying Data from Multiple Tables Oracle By Rana Umer NonEqui joins: On NonEqui Join Condition does with out ‘=‘ sign SELECT e.last_name, e.salary, j.grade_level FROM employees e JOIN job_grades j ON e.salary BETWEEN j.lowest_sal AND j.highest_sal; …

14 Displaying Data from Multiple Tables Oracle By Rana Umer OUTER join: LEFT OUTER join RIGHT OUTER join FULL OUTER join

15 Displaying Data from Multiple Tables Oracle By Rana Umer Outer Joins An Inner Join of two tables returning only matched rows. A join between two tables that returns the results of the inner join as well as the unmatched rows from the Left, Right or Full table is called a Left, Right or Full outer join.

16 Displaying Data from Multiple Tables Oracle By Rana Umer Outer Joins LEFT OUTER JOIN … SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON (e.department_id = d.department_id) ;

17 Displaying Data from Multiple Tables Oracle By Rana Umer Outer Joins RIGHT OUTER JOIN … SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON (e.department_id = d.department_id) ;

18 Displaying Data from Multiple Tables Oracle By Rana Umer Outer Joins FULL OUTER JOIN … SELECT e.last_name, d.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON (e.department_id = d.department_id) ;

19 Displaying Data from Multiple Tables Oracle By Rana Umer Creating Cross Joins – The CROSS JOIN clause produces the cross-product of two tables. – This is also called a Cartesian product between the two tables. … SELECT last_name, department_name FROM employees CROSS JOIN departments ;


Download ppt "SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City."

Similar presentations


Ads by Google