Kuest AI

Study anything, free

Start free

SQL Basics: Tables, Rows, Columns, and SELECT

Learn how relational databases organize data in tables and how to retrieve data with your first SQL queries.

What SQL and databases do

SQL (Structured Query Language) is used to work with relational databases. A database stores organized information, usually in related tables.

A table is similar to a spreadsheet, but it has a defined structure:

customer_idcustomer_namecity
101Amina LeeBristol
102Rafael CruzLeeds
  • A table holds one type of entity, such as customers.
  • A row is one complete record, such as one customer.
  • A column stores one kind of attribute, such as city.
  • A primary key is a column whose value uniquely identifies each row, such as customer_id.

Creating a table and inserting records

A table is created with CREATE TABLE. Each column needs a name and a data type.

sql
CREATE TABLE customers (  customer_id INTEGER PRIMARY KEY,  customer_name TEXT,  city TEXT);

Here, customer_id is the primary key. INTEGER stores whole numbers, while TEXT stores words and other character data.

Add rows with INSERT INTO:

sql
INSERT INTO customers (customer_id, customer_name, city)VALUES  (101, 'Amina Lee', 'Bristol'),  (102, 'Rafael Cruz', 'Leeds');

Listing column names in the INSERT statement makes it clear which value belongs in each column.

Reading data with SELECT

Use SELECT to retrieve data from a table.

sql
SELECT customer_nameFROM customers;

This returns only the customer_name column:

customer_name
Amina Lee
Rafael Cruz

To retrieve several columns, separate them with commas:

sql
SELECT customer_name, cityFROM customers;

To retrieve every column, use *:

sql
SELECT *FROM customers;

SELECT * is useful for quick exploration, but naming the needed columns is usually clearer and avoids retrieving unnecessary data.

Worked example: finding a specific group of rows

Suppose the table contains customers in several cities:

sql
INSERT INTO customers (customer_id, customer_name, city)VALUES  (103, 'Noah Patel', 'Bristol');

To show only customers in Bristol, add a WHERE condition:

sql
SELECT customer_name, cityFROM customersWHERE city = 'Bristol';

Step by step:

  1. FROM customers chooses the table.
  2. WHERE city = 'Bristol' keeps only matching rows.
  3. SELECT customer_name, city chooses the displayed columns.

Result:

customer_namecity
Amina LeeBristol
Noah PatelBristol

Text values are normally enclosed in single quotes. Column and table names are not.

Check yourself

  1. 1.

    What is the difference between a row and a column in a database table?

    Show the answer

    A row is one complete record, such as one customer. A column is one type of information shared by records, such as city.

  2. 2.

    Write a query that shows every column from the customers table.

    Show the answer
    sql
    SELECT *FROM customers;
  3. 3.

    Write a query that returns only customer_name and city from customers.

    Show the answer
    sql
    SELECT customer_name, cityFROM customers;
  4. 4.

    Why is customer_id usually a stronger primary key than customer_name?

    Show the answer

    A primary key must be unique. Different customers can share the same name, while each customer_id is assigned to only one row.

Written with Kuest's AI tutor from a study session and checked before publishing; no personal details are included. Spot a mistake? Tell us.