top of page

Structured Query Language (SQL)

What is SQL?

  • SQL stands for Structured Query Language

  • It is the standard language for interacting with relational databases

  • SQL lets you retrieve, insert, update, and delete data stored in tables

What is a table?

  • A table organises related data into rows and columns

  • Each row (record) contains one entry of data

  • Each column (field) represents a specific attribute, such as id or name

    ree

What is a record?

  • A record (or tuple) is a single entry in a table

  • It consists of one value for each field (column)

What is a field?

  • A field (or column) defines a specific attribute of a record

  • Examples: id, name, age, dept

Main SQL Commands

Command

Description

Example

SELECT (columns)

Retrieves specified columns from a table.

SELECT id, name FROM employees;

FROM (table)

Specifies which table to query.

Used after SELECT, e.g. SELECT * FROM employees;

WHERE (condition)

Filters rows based on a condition.

SELECT * FROM employees WHERE age > 30;

ORDER BY `(column ASC / DESC)`

Sorts result set in ascending or descending order.

SELECT * FROM employees ORDER BY name DESC;

INSERT INTO (table, columns) VALUES(values)

Adds one or more new rows to a table.

INSERT INTO employees (id,name,age,dept) VALUES(21,'Zach',40,'Sales');

UPDATE (table) SET column=value,… WHERE…

Modifies existing rows that match the WHERE condition.

UPDATE employees SET age=31 WHERE name='Zach';

DELETE FROM (table) WHERE(condition)

Removes rows that match the WHERE condition.

DELETE FROM employees WHERE dept='HR';

Examples

  • Retrieve all columns

SELECT * FROM employees;
  • Filter by age and sort descending

SELECT id, name, age
FROM employees
WHERE age >= 30
ORDER BY age DESC;
  • Insert a new employee

INSERT INTO employees (id, name, age, dept) 
VALUES (21, 'Zach', 40, 'Sales');
  • Update department for a specific employee

UPDATE employees 
SET dept = 'Engineering' 
WHERE name = 'Trent';
  • Delete all employees in HR

DELETE FROM employees WHERE dept = 'HR';

Advanced SQL Concepts

  • Logical Operators (AND | OR): Combine multiple WHERE conditions.

    SELECT * FROM employees WHERE dept = 'Sales' AND age > 30;

  • DISTINCT: Removes duplicate values.

    SELECT DISTINCT dept FROM employees;

  • Aggregate Functions + GROUP BY: Perform calculations on groups.

    SELECT dept, COUNT(*) AS count FROM employees GROUP BY dept;

  • JOINs: Combine related data (see above INNER/LEFT/RIGHT JOIN).

  • LIMIT: Restricts the number of rows returned.

    SELECT * FROM employees ORDER BY age DESC LIMIT 5;



bottom of page