All 40 lessons

Lesson 1 of 40 · SQL Basics

What is SQL?

By Skillancy Editorial Team

Every example runs on the ShopKart dataset, already loaded in the free SQL Compiler. Click Try it in the SQL Compiler under a query to open it there, then press Run.

SQL (Structured Query Language) is the standard language for working with data stored in relational databases. You use it to ask a database questions, such as "how many orders did we get last month?", and get the answer back as a table. Almost every company that stores data, from Flipkart-scale e-commerce to a small clinic, runs on a database that understands SQL.

SQL is usually pronounced "S-Q-L" or "sequel". Both are fine.

What is a database?

A relational database stores data in tables. A table looks like a spreadsheet:

  • Each row is one record (one customer, one order).
  • Each column is one attribute of that record (name, city, order date).
  • Tables are linked to each other by shared columns called keys. For example, every order has a customer_id that points to a row in the customers table.

Popular relational databases include PostgreSQL, MySQL, Microsoft SQL Server, Oracle, Snowflake and Google BigQuery. They all speak SQL, with small differences in syntax called dialects.

Your first SQL query

Throughout these tutorials we use ShopKart, a small Indian e-commerce store. Here is a query that lists three customers:

SELECT customer_name, city
FROM customers
LIMIT 3;
Try it in the SQL Compiler

Result:

customer_namecity
Aarav SharmaBengaluru
Priya NairKochi
Rohan GuptaDelhi

(3 rows)

Read it almost like English: select the name and city from the customers table, and limit the output to 3 rows.

Here is a slightly more useful one: how many orders has ShopKart received?

SELECT COUNT(*) AS total_orders
FROM orders;
Try it in the SQL Compiler

Result:

total_orders
24

(1 row)

You will learn exactly how COUNT(*) and AS work in the coming lessons.

What can you do with SQL?

SQL commands fall into a few families:

FamilyWhat it doesMain commands
Querying (DQL)Read dataSELECT
Data manipulation (DML)Add, change, remove rowsINSERT, UPDATE, DELETE
Data definition (DDL)Create and change tablesCREATE, ALTER, DROP
Transaction controlSave or undo changes safelyBEGIN, COMMIT, ROLLBACK
Access control (DCL)Manage permissionsGRANT, REVOKE

As a data analyst you will spend most of your time on SELECT: filtering, grouping, joining tables and calculating metrics.

How SQL works: declarative, not step by step

In Python you write how to do something: loop over rows, check a condition, add to a total. In SQL you describe what you want, and the database engine works out the fastest way to get it.

-- "Give me total revenue from delivered orders"
SELECT SUM(oi.quantity * oi.unit_price)
FROM order_items oi
JOIN orders o ON o.order_id = oi.order_id
WHERE o.status = 'Delivered';

You never tell the database which rows to read first or how to match the tables. That is the job of its query optimizer.

SQL syntax basics

  • SQL keywords are not case-sensitive: SELECT, select and Select all work. By convention we write keywords in capitals.
  • Statements end with a semicolon ;.
  • Text values go in single quotes: 'Bengaluru'. Double quotes are for column or table names.
  • Comments start with -- (one line) or are wrapped in /* ... */ (multi-line).
  • Line breaks and extra spaces don't matter, so format queries to be readable.

Common mistakes

  • Using double quotes for text. WHERE city = "Delhi" fails in PostgreSQL because double quotes mean a column name. Use 'Delhi'.
  • Expecting a fixed row order. Without ORDER BY, the database can return rows in any order.
  • Learning a dialect instead of SQL. Focus on standard SQL first; dialect differences are small and easy to look up.

Practice question

Write a query that shows the product_name and price of 5 products.

Show solution
SELECT product_name, price
FROM products
LIMIT 5;
Try it in the SQL Compiler

Result:

product_nameprice
Redmi Note 13 Pro23999.00
boAt Airdopes 1411299.00
Samsung 43" Smart TV31990.00
Men's Cotton Kurta1899.00
Women's Silk Saree8499.00

(5 rows)

Key takeaways

  • SQL is the standard language for querying relational databases.
  • Data lives in tables made of rows and columns, linked by keys.
  • SELECT ... FROM ... is the starting point of almost every query.
  • SQL is declarative: describe the result, and the database figures out how.

Frequently asked questions

What is SQL used for?

SQL is used to read, filter, summarise and change data stored in relational databases. Data analysts use it to answer business questions such as monthly revenue, top-selling products or repeat-customer rates.

Is SQL a programming language?

SQL is a declarative query language. You describe the result you want and the database decides how to compute it. It is not a general-purpose language like Python, but it is the standard language for working with relational data.

Is SQL hard to learn?

No. Most beginners write useful queries within a day. SELECT, WHERE, GROUP BY and JOIN cover most day-to-day analyst work, and you can learn them in a couple of weeks of regular practice.

Which SQL should I learn: MySQL, PostgreSQL or SQL Server?

Learn standard SQL first. About 90% of what you write is the same everywhere. These tutorials use PostgreSQL and point out where MySQL or SQL Server differ.

Newsletter

Keep learning SQL

One email a week with SQL and analytics guides, new practice questions and cohort dates. No spam, unsubscribe anytime.

Prefer a one-page summary? Download the free SQL cheat sheet (PDF)