Friday, 21 February 2014

Writing Basic SQL SELECT Statements


  • Show the structure of the DEPARTMENTS table. Select all data from the table.
DESC departments;
SELECT * 
FROM departments;


  • Show the structure of the EMPLOYEES table. Write a query to display the last name, job code, hire date and employee number for each employee, with employee number appearing first
DESC employees;
SELECT employee_id, last_name, job_id, hire_date 
FROM employees; 


  • Create a query to display unique job codes from the EMPLOYEES table. 
SELECT DISTINCT job_id
FROM employees;


  • Write a query to display the column headings Emp #, Employee, Job, and Hire Date, respectively, from the EMPLOYEES table. 
SELECT employee_id "Emp #", last_name "Employee", job_id "Job", hire_date "Hire Date" 
FROM employees;
Note: You can also use AS to display the column with other name. For ex: in above query you can also write: SELECT employee_number AS "Emp #" from emp; 


  • Display the last name concatenated with the Job ID, separated by a comma and space, and name the column Employee and Title.
SELECT last_name||', '||job_id "Employee and Title" 
FROM employees;


  • Create a query to display all the data from the EMPLOYEES table. Separate each column by a comma. Name the column THE_OUTPUT. 
SELECT employee_id||','||last_name||','||job_id||','||manager_id||','||hire_date||','||salary||','||commission_pct "THE_OUTPUT" 
FROM employees;

No comments:

Post a Comment