Get Started with SQL

SQL, or Structured Query Language, is a computer language aimed at querying relational databases. This post will show you how to get started with SQL, including learning the basic syntax of selecting, adding, updating and removing records from a database.

Firstly, note that SQL is case insensitive, which means using SELECT or select have the same meaning. Any SQL commands should also be terminated with a semi-colon.

SQL SELECT

To select information from a database table, use the SELECT statement.

SELECT * FROM table_name;

The * indicates that all columns will be selected from the table_name database table.

To select individual columns, replace the asterisk with the comma separated list of column names.

SELECT first_name, last_name FROM table_name;

Both the above statements will select all the rows from the given database table. To filter the data retrieved from the table, use the WHERE statement. This will extract records that only match a specified condition.

SELECT first_name, last_name FROM table_name WHERE first_name = 'John';

SQL INSERT

To insert data into a table, use INSERT.

You can specify the column names and values within those columns in two pairs of parentheses. The pair of parentheses specifying the column names should be added after, INSERT INTO table_name, and the parentheses specifying the column values should be added after VALUES.

INSERT INTO table_name (first_name, last_name) VALUES (value1, value2);

You may not need to specify column names in the query if adding values for all the columns of the table. Ensure that the order of the values is in the same order as the columns in the table within the statement.

INSERT INTO table_name VALUES (value1, value2, value3);

SQL UPDATE

To update existing information in a table, UPDATE is used. Care should be taken when using this query, as omitting a WHERE clause will cause all records to be modified with the specified information.

UPDATE table_name SET first_name = 'Jane', last_name = 'Doe' WHERE user_id = 1;

SQL DELETE

Use DELETE to delete information within a table.

Extra care should be taken when using this query! Omitting a WHERE clause using this query will delete ALL records from the database table.

DELETE FROM table_name WHERE first_name = 'John';

DELETE FROM table_name;  /*This line will delete all records!*/