Skip to content

Basics

Basic Syntax

SQL statements are made up of clauses, which are made up of keywords and expressions. Clauses are separated by semicolons. By convention, keywords are written in uppercase, and expressions are written in lowercase.

Modifying Data

The SELECT clause is used to read data from a database. It is made up of two parts: the SELECT keyword and the FROM keyword. The SELECT keyword is used to specify which columns to read. The FROM keyword is used to specify which table to read from.

SELECT * FROM users;

Will return:

first_namelast_nameage
JohnDoe30
JaneDoe28
BobSmith35

The above statement is made up of two clauses: SELECT * FROM users and ;. The first clause is a SELECT statement, which is used to read data from a database. The second clause is a semicolon, which is used to terminate the statement.

The * is a wildcard, which means “all columns”. The users is the name of the table to read from. You can also specify which columns to read:

SELECT first_name, last_name FROM users;

The above statement will return:

first_namelast_name
JohnDoe
JaneDoe
BobSmith
BettySmith

Inserting Data

We can insert a new user with the following statement:

INSERT INTO users (first_name, last_name, age) VALUES ('Betty', 'Smith', 32);

The above statement is made up of two clauses: INSERT INTO users (first_name, last_name, age) and VALUES ('Betty', 'Smith', 32). The first clause is an INSERT statement, which is used to insert data into a database. The second clause is a VALUES clause, which is used to specify the values to insert.

The table will now be updated to be:

first_namelast_nameage
JohnDoe30
JaneDoe28
BobSmith35
BettySmith32

Updating Data

We can update a user’s age with the following statement:

UPDATE users SET age = 31 WHERE first_name = 'John' AND last_name = 'Doe';

The above statement is made up of two clauses: UPDATE users SET age = 31 and WHERE first_name = 'John' AND last_name = 'Doe'. The first clause is an UPDATE statement, which is used to update data in a database. The second clause is a WHERE clause, which is used to specify which rows to update.

The table will now be updated to be:

first_namelast_nameage
JohnDoe31
JaneDoe28
BobSmith35
BettySmith32

Deleting Data

We can delete a user with the following statement:

DELETE FROM users WHERE first_name = 'John' AND last_name = 'Doe';

The above statement is made up of two clauses: DELETE FROM users and WHERE first_name = 'John' AND last_name = 'Doe'. The first clause is a DELETE statement, which is used to delete data from a database. The second clause is a WHERE clause, which is used to specify which rows to delete.

The table will now be updated to be:

first_namelast_nameage
JaneDoe28
BobSmith35
BettySmith32