SQLWorksheet2Notes.doc - SQL Worksheet 2 Notes JOINING TABLES It is necessary to join two or more tables for some queries This takes place by

SQLWorksheet2Notes.doc - SQL Worksheet 2 Notes JOINING...

This preview shows page 1 - 2 out of 4 pages.

SQL Worksheet 2 Notes JOINING TABLES It is necessary to join two or more tables for some queries. This takes place by establishing a relationship (usually equality) between a column (domain) present in two tables known as a foreign key. Simple joins are usually called equi- joins. A join is automatically performed when a reference is made to more than one table in the FROM clause. SELECT ename, sal, loc FROM emp, dept WHERE ename = 'ALLEN' (search condition) AND emp.deptno = dept.deptno; ((join condition) Find Allen's name and salary from the EMP table and location of Allen's department from the DEPT table. N.B. because we are now referencing two tables which each have a column with the same name (deptno), we must always qualify deptno with its table name in order to prevent confusion, this qualification must be used whenever ambiguous column names are used within an SQL statement. SELECT ename, dname FROM emp, dept WHERE emp.deptno = dept.deptno ORDER BY ename; List the name and department of all employees in name order. (This joins the two tables over DEPTNO and projects out ENAME and DNAME) Abbreviating Table Names. Table names can be abbreviated in order to simplify what is typed in with the query. In this example E and D are abbreviated names for emp and dept. List the department name and all employee data for employees that work in Chicago; SELECT dname, E.* FROM emp E, dept D WHERE E.deptno = D.deptno AND loc = 'LUTON' ORDER BY E.deptno;
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read all 4 pages?

  • Spring '14

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors