Previous Topic: Sample SQL StatementsNext Topic: Update Statements


Select Statements

Now that we've defined the tables, let's see some possible ways to use them. To display information about an employee, the SQL Select statement for the EMPLOYEE table might look like this:

SELECT * FROM EMPDB.EMPLOYEE
WHERE LAST_NAME_0415 = 'MARKEY';

This statement returns all columns for each row found whose last name equals Markey. The result table would also contain the foreign keys for the Department and Office tables. Therefore, you can also write a query that returns rows where employee Markey works for department 411:

SELECT * FROM EMPDB.EMPLOYEE
WHERE LAST\-_NAME_0415 = 'MARKEY'
AND DEPT_ID_0410 =  411;

So far you have only used data from the single table EMPDB.EMPLOYEE. Let's say you want to retrieve the employee Markey who works in the SHIPPING department. The EMPLOYEE table contains the foreign key to the DEPARTMENT table. You can join the two tables to form a query using the department name:

SELECT EMP.LAST_NAME_0415, EMP.FIRST_NAME_0415
FROM EMPDB.EMPLOYEE AS EMP,
EMPDEMO.DEPARTMENT AS DEPT
WHERE LAST\-_NAME_0415 = 'MARKEY'
AND EMP.DEPT_ID_0410 =  DEPT.DEPT_ID_0410
AND DEPT.DEPT_NAME_0410 = 'SHIPPING';

Note: The Department table is from the EMPDEMO schema. You used a table from a non‑SQL schema in this query.