An intro to SQL commands

SQL queries are written to be able to perform CRUD (create, read, update, delete) actions with your database tables. This queries can become much more complex as the relationships of your tables increase in complexity.

As with any computer language, the best way to truly learn it is with practice. So I will show a number of examples of how to use the CREATE TABLE, FROM, SELECT, WHERE, and AND/OR keywords with SQL.

CREATE TABLE

CREATE TABLE is as simple as it sounds. This command adds a new table to your database. More specifically, it allows you to name the table and to name each column within the table.

If I were to use create table for the customers table in the schema pictured above it would look like this:

CREATE TABLE customers (

id INT PRIMARY KEY,

first_name TEXT,

last_name TEXT,

email TEXT

);

Convention dictates that all SQL keywords are uppercased. You will also notice that each column is on a new line, this another best practice. While not necessary, both conventions make it far more readable to you and I.

FROM and SELECT

The FROM keyword is used to dictate which table you are accessing and the SELECT keyword is used to determine which columns you would like to see.

SELECT * FROM customers;

If there were five customers in the table in our database, the above query is requesting all column information * from the customers table. And the result from that query would look like this:

If instead, I just wanted the emails I would modify my SQL query to read:

SELECT email FROM customers;

The return values would then be all five of the emails:

WHERE

The WHERE keyword indicates that you want to filter the results given a set of parameters. Only results where the condition you set evaluates to true will be returned.

SELECT * FROM customers

WHERE id >= 3;

The above query selects all rows from the customers table where the id is greater than or equal to three. The return value for this query would look like: