- In this section, you execute the SELECT statement to query tables in the HR schema. You also use the ORDER BY and WHERE clauses within the SELECT statement to sort and restrict data in the result set.
- A SELECT clause, which specifies columns containing the values to be matched
- A FROM clause, which specifies the table containing the columns listed in the SELECT clause
Querying Tables
- In this section, you execute the SELECT statement to retrieve data from tables and views. You can select rows and columns that you want to return in the output. In its simplest form, a SELECT statement must contain the following:
FROM <table>;
SELECT *
FROM departments;
SELECT job_id, job_title
FROM jobs;
Restricting Data
- In this section, you use the WHERE clause to restrict the rows that are returned from the SELECT query. A WHERE clause contains a condition that must be met. It directly follows the FROM clause. If the condition is true, the row that meets the condition is returned.
SELECT *
FROM departments
WHERE department_id=60;
Sorting Data
- In this section, you use the ORDER BY clause to sort the rows that are retrieved from the SELECT statement. You specify the column based on the rows that must be sorted. You also specify the ASC keyword to display rows in ascending order (default), and you specify the DESC keyword to display rows in descending order.
SELECT last_name, job_id, hire_date
FROM employees
WHERE job_id='SA_REP'
ORDER BY hire_date;
SELECT last_name, job_id, hire_date
FROM employees
WHERE job_id='SA_REP'
ORDER BY hire_date DESC;