The document discusses different types of joins that can be used in SQL queries:
1) Natural joins, using clauses, table aliases, and the ON clause are described for performing inner joins to retrieve records from multiple tables.
2) Self-joins using the ON clause are demonstrated to join a table to itself.
3) Additional conditions can be added to joins using the AND operator.
4) Three-way joins can be created using the ON clause to join three tables together.
5) Outer joins including left, right, full and cross joins are explained and examples are provided.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
26 views4 pages
Retrieving Records With Natural Joins
The document discusses different types of joins that can be used in SQL queries:
1) Natural joins, using clauses, table aliases, and the ON clause are described for performing inner joins to retrieve records from multiple tables.
2) Self-joins using the ON clause are demonstrated to join a table to itself.
3) Additional conditions can be added to joins using the AND operator.
4) Three-way joins can be created using the ON clause to join three tables together.
5) Outer joins including left, right, full and cross joins are explained and examples are provided.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4
Retrieving Records with Natural Joins
SELECT department_id, department_name,
location_id, city FROM departments NATURAL JOIN locations select ! "rom departments select ! "rom locations ##$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ Retrieving Records with the USING Clause SELECT employees.employee_id, employees.last_name, departments.location_id, department_id FROM employees JOIN departments USIN !department_id" # select $ %rom departments# select $ %rom employees# &&'''''''''''''''''''''''''''''''''''''' Using Table Aliases SELECT e%employee_id, e%last_name, d%location_id, department_id FROM employees e JOIN departments d USIN& 'department_id( &&'''''''''''''''''''''''''''''''''''''' Retrieving Records with the ON Clause 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( &&'''''''''''''''''''''''''''''''''''''' Self-Joins Using the ON Clause SELECT e.last_name emp, m.last_name m(r FROM employees e JOIN employees m ON !e.mana(er_id ) m.employee_id"# &&'''''''''''''''''''''''''''''''''''''' Applying Additional Conditions to a Join 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( AN* e%mana+er_id ) ,-- &&'''''''''''''''''''''''''''''''''''''' Creating Three-Way Joins with the ON Clause SELECT employee_id, city, department_name FROM employees e JOIN departments d ON d%department_id ) e%department_id JOIN locations l ON d%location_id ) l%location_id &&'''''''''''''''''''''''''''''''''''''' Retrieving Records with Noneui!oins ""Se crea la tabla !ob#grades asi$ create table !ob#grades %&RA'(#)(*() C+AR ,R-.AR/ 0(/1 )2W(ST#SA) NU.3(R1 +-&+(ST#SA) NU.3(R45 &&SE INSERT*N +*TOS ,EIN INSERT INTO JO,_R*+ES -*LUES!.*.,/000,1222"# INSERT INTO JO,_R*+ES -*LUES!.,.,3000,4222"# INSERT INTO JO,_R*+ES -*LUES!.C.,5000,2222"# INSERT INTO JO,_R*+ES -*LUES!.+.,/0000,/6222"# INSERT INTO JO,_R*+ES -*LUES!.E.,/4000,16222"# INSERT INTO JO,_R*+ES -*LUES!.F.,14000,60000"# EN+# SELECT $ FROM JO,_R*+ES# SELECT e.last_name, e.salary, 7.(rade_le8el FROM employees e JOIN 7o9_(rades 7 ON e.salary ,ET:EEN 7.lo;est_sal *N+ 7.<i(<est_sal# &&'''''''''''''''''''''''''''''''''''''' &&SE CRE*N L*S T*,L*S CLIENTE = -EN+E+OR SIN REL*CIONES *SI#
CRE*TE T*,LE CLIENTE ! cod_cliente n>m9er, nom_cliente 8arc<ar1!60", cod_8endedor n>m9er "# CRE*TE T*,LE -EN+E+OR ! cod_8endedor n>m9er, nom_8endedor 8arc<ar1!60" "# && SE INSERT*N LOS +*TOS ,EIN INSERT INTO CLIENTE -*LUES !/, .J*IME., /0"# INSERT INTO CLIENTE -*LUES !1, .JOSE., 10"# INSERT INTO CLIENTE -*LUES !3, .JORE., 30"# INSERT INTO CLIENTE -*LUES !6, .JU*N., 60"# INSERT INTO CLIENTE -*LUES !4, .J*CINTO., 40"# EN+# ,EIN INSERT INTO -EN+E+OR -*LUES !/0, .S*N+R*."# INSERT INTO -EN+E+OR -*LUES !10, .CL*U+I*."# INSERT INTO -EN+E+OR -*LUES !30, .=*M=LE."# INSERT INTO -EN+E+OR -*LUES !50, .*LF*."# INSERT INTO -EN+E+OR -*LUES !?0, .CRIST*L."# EN+# LEFT OUTER JOIN SELECT 8.nom_8endedor, c.nom_cliente FROM -EN+E+OR 8 LEFT OUTER JOIN CLIENTE c ON !8.cod_8endedor ) c.cod_8endedor"# &&'''''''''''''''''''''''''''''''''''''' RIGHT OUTER JOIN SELECT v.nom_vendedor, c.nom_cliente FROM VENEOR v RIGHT OUTER JOIN CLIENTE c ON !v.cod_vendedor " c.cod_vendedor#$ &&'''''''''''''''''''''''''''''''''''''' FULL OUTER JOIN SELECT 8.nom_8endedor, c.nom_cliente FROM -EN+E+OR 8 FULL OUTER JOIN CLIENTE c ON !8.cod_8endedor ) c.cod_8endedor"# &&'''''''''''''''''''''''''''''''''''''' SELECT nom_8endedor, nom_cliente FROM -EN+E+OR CROSS JOIN CLIENTE # SELECT $ FROM -EN+E+OR# SELECT $ FROM CLIENTE#