Basic SQL Syntax
In this lesson, we’ll dive into the basic syntax of SQL and the SELECT statement.
SQL commands are used to query and manage data stored in relational databases. SQL commands can be broadly divided into three categories:
- Data definition language (DDL)
- Data manipulation language (DML)
- Data control language (DCL)
Throughout this course, we will be focusing more on the DML part of SQL, which is what you need to querying, update, and analyze data. To begin, let's start with the SELECT statement.
Selecting data with SELECT
The SELECT statement is used to query the database and retrieve information from it. Here's the basic syntax of the SELECT statement:
SQLSELECT column_name(s) FROM table_name;
In the above example, the SELECT statement specifies which columns we want to retrieve, and the FROM clause specifies which table we want to retrieve the information from. If we want to retrieve all columns from the table, we can use the * symbol instead of listing out each column name.
For example, to retrieve everything in the customers table, we would run the following SQL query:
SQLSELECT * FROM customers;
Sometimes, we may not want or need to select all the columns in the table, but only specific columns of interest. In this case, we can specify the column names after the SELECT statement, separated by a comma. For example, to select only the id and name columns from the customers table, we would use the following query:
SQLSELECT id, name FROM customers;
The output of the query will contain the columns you specified, in the same order as your query, like so:
In some cases, you may want to rename the result columns to something else. Luckily, it’s easy to do so with the AS keyword:
SQLSELECT id, name AS customer_name FROM customers;
which would output the following result table:
Try it yourself
Write a query for the customers table and select all rows and columns in the table:
Reminders
As a general syntax reminder, SQL keywords are not case sensitive, meaning that you can type “SELECT”, “select”, or even “SelEcT,” but table names and column names are case sensitive in some contexts. SQL also isn’t whitespace-sensitive, meaning that you can use line breaks and indentations if you want. For the sake of readability and convention, in this course we will show keywords in UPPERCASE, tables and column names in lowercase or “snake_case”, and whitespace where helpful.
In the next lesson, we will learn how to filter data using the WHERE clause.