Menu

Databases and Your First SQL Query

Lesson 1: Databases and Your First SQL Query

What is a relational database?

A relational database stores related information in tables.

Term

Meaning

Example

TableA collection of related recordscustomers, orders
Row (record)One itemOne customer
Column (field)One attributecity
Primary keyUniquely identifies each rowcustomer_id in customers
Foreign keyLinks to another table's primary keycustomer_id in orders
SchemaThe structure of tables and their relationshipsColumn names, types, keys

Example relationship:

  • customers (customer_id, customer_name, city)
  • orders (order_id, customer_id, order_date, total_amount)
  • One customer → many orders (a one-to-many relationship)

Example

To retrieve customer names and cities:

SELECT customer_name, city
FROM customers;

customer_name

city

Asha RChennai
Vikram SBengaluru

How it works

Part

Role

SELECTChooses the columns to return
FROMNames the source table
;Ends the statement in many SQL tools
  • SQL keywords are commonly written in uppercase for readability. Most systems don't require it.
  • You can rename output columns with AS: SELECT customer_name AS name FROM customers;
  • SELECT DISTINCT city FROM customers; returns each city once.

Common mistakes

  • Misspelled table or column names – check the schema first.
  • Using SELECT * in real analysis when you need only a few fields:
    • Slows queries on large tables
    • Can be expensive on pay-per-query platforms such as BigQuery
    • May expose sensitive data you don't need
  • Forgetting commas between column names.
  • Using double quotes for text values. Many systems treat double quotes as identifiers. Use single quotes for text.

Try it: Write a query that returns order_id, order_date, and total_amount from a table named orders.


Data Analytics Tutorial



Learn via Video Course

Data Analysis and Processing with Python using Pandas - Data Science Course Logo

Data Analysis and Processing with Python using Pandas - Data Science Course

5 hrs



Get in Touch For Details