SQL > SQL Tutorial Videos > SQL Basics Video

This video covers the following SQL keywords: SELECT ... FROM, DISTINCT, WHERE, ORDER BY, GROUP BY, and HAVING. After you finish watching this video, you'll be able to write basic SQL queries to retrieve data from a relational database.

Embed this video using the code below

Video Transcription

Welcome to the SQL Tutorial, presented to you by 1Keydata.com. In this tutorial, we will go over the basics of SQL, so you'll be able to use SQL to write simple queries to get data from a relational database. We will first provide some background information on how data gets stored in a relational database. Next we will go over the basic SQL commands. In particular, you will learn about:


In a relational database, data is stored in tables. Each table has a table name. A table consists of columns and rows. Each column is a field in a record, and there is a column name associated with each column. Each row represents one record. Usually when we say how many records we have, we are referring to the number of rows. With this information, now we are ready to dive into SQL.

SQL is structured very much like the English language. At the high level, the basic command for retrieving data from a database table is to SELECT data FROM a table. Not surprisingly, the keywords "SELECT" and "FROM" make up the core of the SQL statement. In fact, the syntax for SELECT FROM is pretty straightforward: SELECT COLUMN_NAME FROM TABLE_NAME.

We can select one or more columns. To select more than one column, we simply list all the column names we want to select separated by comma. If we want to select all columns, we say SELECT *.

If we want to select unique values, we add the keyword DISTINCT in front of the column name.

Next we look at the WHERE keyword. In the previous example, we are retrieving all records from a table. While we do want to do this sometimes, more frequently we want to select only a subset of the data. When we want to filter our results, we use the WHERE keyword after the FROM keywords. So the syntax becomes


Condition is where we specify how we want the data to be filtered. For example, we may specify that only people whose first name starts with "A" is selected.

Now we turn our attention to the ORDER BY keyword.

Sometimes we want the output to be listed in a particular order, such as in ascending order or in descending order. To achieve this, we use the OREDER BY keyword. The syntax for this will be something similar to the following:


Notice that ORDER BY is at the end of the statement, because we want to specify the ordering after we have specified what data we want to get.

To show results in ascending order, i.e., from A to Z or from smaller values to larger values, we can add ASC at the end. To show results in descending order, i.e., from Z to A or from larger values to smaller values, we can add DESC at the end. Usually if you do not specify this, the default is ascending. Next we discuss the GROUP BY keyword.

Before we go into details about GROUP BY, I'd like to talk first about functions. The typical mathematical functions you see in SQL are SUM, AVG, COUNT, MAX, and MIN. Functions allow us to operate on the values of specific columns. For example, the MAX function allows us to retrieve the largest value in a column.

Let's say we have a table that has three columns: Date, Store, and Sales Amount. To find out what was the highest sales amount across all dates and all stores, we can simply use SELECT MAX(Sales Amount). On the other hand, if we want to find out the highest sales amount for each store, then we'll need to retrieve both the Store column and the sales amount column, and our SELECT statement becomes

SELECT Store, MAX(Sales Amount)

However, this SELECT statement by itself is not enough. To allow SQL to correctly calculate what we want, we need to use the GROUP BY keyword. Here, we will add


to let SQL know that we want to calculate the maximum sales amount for each store. So how the function is calculated is dependent on the column or columns specified in the GROUP BY, not the column or columns specified in the SELECT. Often the columns in the GROUP BY phrase are same as the columns in the SELECT statement, but this is not always the case.

To recap, the general syntax for GROUP BY is


Previously we had talked about using the WHERE keyword to filter results. What if we want to filter based on the result of a function? Can we also use WHERE for this? The answer is no, and the reason is that we will need to specify the filtering condition after SQL has calculated the function, and as a result any filtering condition based on the function needs to be specified after the GROUP BY phrase. So we cannot use the WHERE keyword because it is always used before GROUP BY. This is where the HAVING keyword comes in. The syntax for HAVING is

HAVING (Condition based on Function)

In the example we had previously mentioned, if we only want to show results where the maximum sales amount is over 100, we will add

HAVING (sales Amount > 100) at the end of the SQL query.

In this tutorial we have introduced the SELECT FROM, WHERE, ORDER BY, GROUP BY, and HAVING keywords. In a SQL statement, what order do we put them? The correct order is SELECT FROM, then WHERE, then GROUP BY, then HAVING, and finally ORDER BY.

That concludes this SQL tutorial. Thank you for viewing. Now you have the knowledge to write basic SQL statements to get data out of a relational database. You can learn more about SQL by going to the 1Keydata SQL Tutorial at www.1keydata.com/sql/sql.html.

List of SQL Video Tutorials

 Title  Description 
 SQL Basics Tutorial Video  How to use SQL to retrieve data from a relational database. 
 Union, Intersect, Minus Video  How to combine two sets of results using SQL. 
 Table Management Video  How to manage database tables in SQL. 
 Table Constraint Video  How to enforce constraints on a column using SQL. 
 Database View Video  What is a database view and how to use it. 

This page was last updated on June 19, 2023.

Copyright © 2024   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact