Use Sophia to knock out your gen-ed requirements quickly and affordably. Learn more
×

SQL Clauses

Author: Sophia

what's covered
"SQL" stands for "Structured Query Language," a programming language that is typically used in relational databases. There are three main SQL clauses: SELECT, FROM, and WHERE. At the conclusion of this tutorial, you will be able to recall the three main SQL clauses and their functions. This tutorial explores these SQL clauses in three parts:
  1. Getting Started
  2. Running a Query
  3. Adding WHERE

1. Getting Started

A database is an organized collection of data. Databases are a useful means of storing, managing, and retrieving data quickly. They consist of sets of records contained within one or more tables, with each table identified by the table name. This unit will teach you how to use simple SQL database commands to manage and retrieve data.

The most common command to interact with a database is the SELECT statement, utilized to query (i.e., request) data from the database. A statement is a complete piece of code that can run independently, and is comprised of clauses. The most basic SELECT statement has the format of:


SELECT * 
FROM <tablename>;

This basic SELECT statement is comprised of two clauses that must occur in that specific order. The SELECT clause indicates the columns to list in the specific order that they will appear. In this case, the * is a unique wildcard that stands for ALL columns. Using this means that all the columns are selected to be displayed back to the user in the order that they appear in the table names that are listed. The second clause is the FROM clause that lists the table names that will be returned. For now, we will only focus on one table, but we will address multiple table queries later on in the course.

The data that is returned from a SELECT statement is stored in a result table called a result-set.

Keep in mind that SQL keywords are not case sensitive, meaning that select is the same as Select or SELECT. Using uppercase keywords is a best practice to visually separate out the keywords from the table or column names. However, it is not required.

terms to know

Database
A set of records contained within one or more tables.
SELECT
An SQL clause that retrieves zero or more rows of data from one or more database columns.
FROM
An SQL clause that identifies one or more tables as the source for a database query statement.

2. Running a Query

Throughout this course, you will use a free and open-source relational database management system called Postgres to practice running queries. To access the Postgres database environment, open the following link in another browser window or tab: https://postgres.sophia.org. If you enter the Postgres database environment, you will see a list of database tables on the left side under the Schema Browser (album, artist, customer, employee, genre, invoice, etc.) Under each table name is a list of columns of data and their data types.

Using the Postgres database environment for this course, you can query the customer table by entering in:


SELECT *
FROM customer;

Then click on the run/play button at the top right.

This will execute the query and return the result set from the query. You should see that there are 59 rows of data. Each row is a unique record from the customer table. Each column name is listed, along with the data associated with it.

This is a useful way to be able to see all of the data in a single table at once.

Video Transcript

try it
Your turn! Try a query in the SQL tool. Start with the example above to see if you can replicate the results. Then, try a SELECT query on any of the other tables listed in the database on the left side under the Schema Browser. To get started, press the LAUNCH DATABASE button below to open the SQL tool in a new tab.

Source: Vincent Tran

Video Transcript


3. Adding WHERE

The third main clause of the SELECT statement is the WHERE clause. The WHERE clause is used to filter records, and only returns those rows/records when they fulfill a specific condition. There are many ways to filter data using the WHERE clause, which can be used not only in SELECT statements, but also in other statements like the UPDATE and DELETE statements. We will cover these other statements in later lessons.

think about it
What might be some other things you’d want to ask that are not possible with only these three clauses?

term to know

WHERE
An SQL clause that applies conditions to filter the result-set.
summary
There are three main SQL clauses: SELECT, FROM, and WHERE. You can use them to form queries that display the desired data from a database. SELECT defines the columns to return from the table. FROM identifies the desired table. WHERE filters the data to meet your chosen conditions.

Source: Authored by Vincent Tran