0% found this document useful (0 votes)
18 views

SQL SELECT - Basic Queries-020425-064414

This document outlines the basics of SQL SELECT statements, including syntax and usage for retrieving data from a database. It covers how to select all or specific columns, filter data with the WHERE clause, and sort results using the ORDER BY clause, with real-life examples from a company database. Additionally, it includes practice exercises to reinforce learning of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL SELECT - Basic Queries-020425-064414

This document outlines the basics of SQL SELECT statements, including syntax and usage for retrieving data from a database. It covers how to select all or specific columns, filter data with the WHERE clause, and sort results using the ORDER BY clause, with real-life examples from a company database. Additionally, it includes practice exercises to reinforce learning of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Notes: SQL SELECT - Basic Queries

I. Learning Expectations:

By the end of this session, trainees will be able to:


Understand the purpose and syntax of the SQL SELECT statement.
Construct queries to retrieve all columns or specific columns from a table.
Apply the WHERE clause to filter data based on given conditions.
Use the ORDER BY clause to sort query results in ascending or descending order.
Write and execute basic SQL SELECT queries on the company_db database.

II. Introduction to the SELECT Statement:

The SELECT statement is the fundamental SQL command used to retrieve data from one or more tables in a database.
It allows you to specify which data you want to retrieve and how you want it to be organized.
Syntax:

1 SELECT column1, column2, ...


2 FROM table_name
3 WHERE condition
4 ORDER BY column1 [ASC | DESC];

SELECT : Specifies the columns you want to retrieve.

FROM : Specifies the table you want to retrieve data from.

WHERE (optional): Filters the data based on a condition.

ORDER BY (optional): Sorts the data.

III. Retrieving Data:

Retrieving All Columns:


To retrieve all columns from a table, use the * wildcard.
Syntax:

1 SELECT * FROM table_name;

Example:
Consider the employees table in the company_db database, which has columns like emp_id , first_name , last_name ,
email , hire_date , etc.

To retrieve all data from the employees table:

1 SELECT * FROM employees;

This will return all rows and all columns from the employees table.
Selecting Specific Columns:
To retrieve specific columns, list the column names separated by commas after the SELECT keyword.
Syntax:

1 SELECT column1, column2, ... FROM table_name;

Example:
To retrieve only the first_name , last_name , and email columns from the employees table:

1 SELECT first_name, last_name, email FROM employees;

This will return only the specified columns for all employees.

IV. Filtering Data with the WHERE Clause:

The WHERE clause is used to filter rows based on a specified condition.


It allows you to retrieve only the data that meets your criteria.
Syntax:

1 SELECT column1, column2, ...


2 FROM table_name
3 WHERE condition;

Conditions can include:


Equality ( = )
Inequality ( != or <> )

Greater than ( > ), less than ( < )


Greater than or equal to ( >= ), less than or equal to ( <= )
Logical operators: AND , OR , NOT
BETWEEN operator to specify a range

IN operator to specify a list of values

LIKE operator for pattern matching

Examples (using company_db ):


Equality:
To retrieve employees from the "Marketing" department (assuming dept_name in departments table is "Marketing"):

1 SELECT emp_id, first_name, last_name


2 FROM employees
3 WHERE dept_id = (SELECT dept_id FROM departments WHERE dept_name = 'Marketing');

This example uses a subquery to find the dept_id for "Marketing".


Inequality:
To retrieve projects that are not in the "Research" phase (assuming "Research" is a value in a phase column in the projects
table):

1 SELECT project_name
2 FROM projects
3 WHERE phase != 'Research';
4 -- Or
5 SELECT project_name
6 FROM projects
7 WHERE phase <> 'Research';

Range with BETWEEN:


To retrieve employees hired between January 1, 2023, and December 31, 2023:

1 SELECT first_name, last_name, hire_date


2 FROM employees
3 WHERE hire_date BETWEEN '2023-01-01' AND '2023-12-31';

Membership with IN:


To retrieve employees from the "Human Resources" or "Finance" departments:

1 SELECT emp_id, first_name, last_name


2 FROM employees
3 WHERE dept_id IN (SELECT dept_id FROM departments WHERE dept_name IN ('Human Resources', 'Finance'));

Pattern matching with LIKE:


To retrieve employees whose last name starts with "S":

1 SELECT emp_id, first_name, last_name


2 FROM employees
3 WHERE last_name LIKE 'S%';

% is a wildcard that represents any sequence of characters.

Logical Operators:
To retrieve employees from the "Engineering" department with a salary greater than $80,000 (assuming salary information is in a
salaries table):

1 SELECT e.emp_id, e.first_name, e.last_name


2 FROM employees e
3 JOIN salaries s ON e.emp_id = s.emp_id
4 WHERE e.dept_id = (SELECT dept_id FROM departments WHERE dept_name = 'Engineering')
5 AND s.base_salary > 80000;

V. Sorting Data with ORDER BY Clause:

The ORDER BY clause is used to sort the result set in ascending ( ASC ) or descending ( DESC ) order based on one or more columns.
Syntax:

1 SELECT column1, column2, ...


2 FROM table_name
3 WHERE condition
4 ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;

If ASC or DESC is not specified, the default is ascending order ( ASC ).


You can sort by multiple columns. The sorting is done based on the order in which the columns are listed.
Examples (using company_db ):
Sorting employees by last name in ascending order:

1 SELECT emp_id, first_name, last_name


2 FROM employees
3 ORDER BY last_name ASC;
4 -- Or (since ASC is the default)
5 SELECT emp_id, first_name, last_name
6 FROM employees
7 ORDER BY last_name;

Sorting projects by project name in descending order:

1 SELECT project_id, project_name


2 FROM projects
3 ORDER BY project_name DESC;
Sorting employees by department (ascending) and then by hire date (descending):

1 SELECT e.emp_id, e.first_name, e.last_name, d.dept_name, e.hire_date


2 FROM employees e
3 JOIN departments d ON e.dept_id = d.dept_id
4 ORDER BY d.dept_name ASC, e.hire_date DESC;

VI. Real-Life Examples (using company_db context):

HR Department:
Retrieve all employees: SELECT * FROM employees;
Retrieve employee names and their department:

1 SELECT first_name, last_name, d.dept_name


2 FROM employees e
3 JOIN departments d ON e.dept_id = d.dept_id;

Retrieve employees hired after January 1, 2023: SELECT first_name, last_name FROM employees WHERE hire_date > '2023-
01-01';

Retrieve employees sorted by hire date (most recent first): SELECT first_name, last_name, hire_date FROM employees ORDER
BY hire_date DESC;

Project Management:
Retrieve all projects: SELECT * FROM projects;
Retrieve project names and start dates: SELECT project_name, start_date FROM projects;
Retrieve projects in the "Completed" phase: SELECT project_name FROM projects WHERE phase = 'Completed'; (Assuming
'Completed' is a value in a phase column)
Retrieve projects sorted by start date: SELECT project_name, start_date FROM projects ORDER BY start_date;

VII. Practice Exercises (using company_db ):

Given the company_db database:


a. Write a query to retrieve all columns for all departments.
b. Write a query to retrieve only the emp_id , first_name , and last_name columns from the employees table.
c. Write a query to retrieve employees from the "IT Support" department.
d. Write a query to retrieve projects with a budget greater than $200,000. (Assume budget information is in the projects table)
e. Write a query to retrieve employees from the "Engineering" department hired after January 1, 2022.
f. Write a query to retrieve employees sorted by last_name in descending order.
g. Write a query to retrieve projects sorted by start_date in ascending order and then by project_name in ascending order.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy