Skip to main content

Queries

Updated Aug 23, 2019 ·

Overview

SQL helps answer questions within and across relational database tables.

  • Querying book checkouts by a specific patron and date in a library database
  • Comparing salaries across departments in an HR database

SQL is often used alongside other tools like spreadsheets. While spreadsheets work well for smaller, simpler datasets, SQL is better for large, complex datasets, such as those in retail platforms. Organizing data in a database and using SQL queries is more effective.

  • Identifying products with the highest sales or worst reviews
  • Analyzing trends in website traffic, customer reviews, and product sales
  • SQL excels with large amounts of data and complex relationships.

Keywords

Keywords are reserved words that specify the operation we want to perform. The two most common keywords are SELECT and FROM.

  • SELECT specifies which fields to retrieve
  • FROM specifies the table where these fields are located

Below is A SAMPLE patrons table:

card_numnamemember_yeartotal_fine
123456John202115.75
234567Jane20205.00
345678Alice20190.00
456789Bob20227.50
567890Charlie202112.30

To list all library patrons, the query starts with the SELECT statement followed by the FROM statement on the next line. It's best practice to end the query with a semicolon to indicate it's complete.

SELECT name
FROM patrons;

Expected Output:

name
John Doe
Jane Smith
Alice Johnson
Bob Brown
Charlie Davis

Note:

  • Keywords are capitalized, while table and field names are lowercase
  • The result set shows all patron names without altering the database
  • Save SQL code to share results with collaborators

Selecting Multiple Fields

To select multiple fields, list them after the SELECT keyword, separated by commas. The order of the fields in the query determines their order in the result set.

SELECT card_num, name
FROM patrons;
card_numname
123456John Doe
234567Jane Smith
345678Alice Johnson
456789Bob Brown
567890Charlie Davis

Selecting All Fields

To select all fields in a table, use an asterisk (*) instead of listing each field name.

SELECT *
FROM patrons;

Expected Output:

card_numnamemember_yeartotal_fine
123456John Doe202115.75
234567Jane Smith20205.00
345678Alice Johnson20190.00
456789Bob Brown20227.50
567890Charlie Davis201712.30

Aliasing

Renaming columns in the result set can improve clarity or brevity. This is done using aliasing. Below is a sample employees table:

idnamedept_idjob_level_idyear_hired
123456John132021
234567Jane232020
345678Alice212019
456789Bob322022
567890Charlie222021

To select the name and year_hired from the employees table and rename the name column to first_name, use the AS keyword.

SELECT name AS first_name, year_hired
FROM employees;

Expected Output:

first_nameyear_hired
John2021
Jane2020
Alice2019
Bob2022
Charlie2021

Selecting Distinct Records

To get a list of unique values, such as years in which employees were hired, use the DISTINCT keyword. Without DISTINCT, duplicate years may appear in the result set.

SELECT DISTINCT year_hired
FROM employees;

Expected Output:

year_hired
2021
2020
2019
2022

DISTINCT with Multiple Fields

To return unique combinations of multiple fields, list the fields after the DISTINCT keyword. For example, to see the years different departments hired employees, query the dept_id and year_hired fields.

SELECT DISTINCT dept_id, year_hired
FROM employees;

Expected Output:

dept_idyear_hired
12021
22020
22019
32022
22021

As we can see, individual fields may still have repeat values, but each combination is unique. For example, the second. third, and fifth record are all unique combination:

Views

Views are virtual tables created from saved SQL SELECT statements. They store the query code rather than the data, so the view's results update automatically with changes to the underlying data. To create a view, use CREATE VIEW followed by the view name and the AS keyword.

CREATE VIEW employee_hire_years AS
SELECT name, dept_id, year_hired
FROM employees;

Once created, a view can be queried just like a normal table.

SELECT *
FROM employee_hire_years;

Expected Output:

namedept_idyear_hired
John12021
Jane22020
Alice22019
Bob32022
Charlie22021