SQL: Structured Query LanguageSQL FundamentalsSQL Basics and Practical Queries

What is SQL, and what problems does it solve?

Question 2 of 2 · Updated

On this page

SQL (Structured Query Language) is the language used to define, store, retrieve, change, and manage data in relational databases.

Think of an online shopping application. It needs to store customers, products, orders, and payments. SQL lets an application ask questions such as:

  • Which customers have placed orders?
  • Which products cost more than ₹1,000?
  • How much revenue did each product generate?
  • Can we update a customer's email address?
  • Can we remove an order that was created incorrectly?

A relational database stores information in tables. SQL provides a consistent way to work with those tables.

1. Understand the data with a practical example#

Imagine an application that stores customer details. We will create a table named customers.

Create a table#

The CREATE TABLE statement defines the table structure: its name, columns, and data types.

SQL
CREATE TABLE customers ( customer_id INTEGER PRIMARY KEY, customer_name VARCHAR(100) NOT NULL, city VARCHAR(50), email VARCHAR(150), total_spent DECIMAL(10, 2) );

What these columns mean:

  • customer_id: identifies each customer. The primary key must be unique and cannot be NULL.
  • customer_name: stores the customer's name and cannot be NULL.
  • city: stores the customer's city.
  • email: stores the customer's email address.
  • total_spent: stores a monetary amount with two decimal places.

Insert sample records#

The INSERT statement adds rows to the table.

SQL
INSERT INTO customers (customer_id, customer_name, city, email, total_spent) VALUES (1, 'Aarav', 'Bengaluru', 'aarav@example.com', 12500.00), (2, 'Diya', 'Mumbai', 'diya@example.com', 8200.50), (3, 'Kabir', 'Bengaluru', 'kabir@example.com', 15600.00), (4, 'Meera', 'Delhi', 'meera@example.com', 4300.00), (5, 'Ishaan', 'Pune', 'ishaan@example.com', 9800.00);

The table now contains this sample data:

customer_idcustomer_namecityemailtotal_spent
1AaravBengaluruaarav@example.com12500.00
2DiyaMumbaidiya@example.com8200.50
3KabirBengalurukabir@example.com15600.00
4MeeraDelhimeera@example.com4300.00
5IshaanPuneishaan@example.com9800.00

2. Retrieve data using SELECT#

Suppose you want to see all customers.

SQL
SELECT * FROM customers;

To retrieve only customer names and cities:

SQL
SELECT customer_name, city FROM customers;

The result is:

customer_namecity
AaravBengaluru
DiyaMumbai
KabirBengaluru
MeeraDelhi
IshaanPune

Practical point: Prefer selecting the columns you need instead of using SELECT * in application queries. It makes the query's requirements clearer and avoids retrieving unnecessary columns.

3. Filter records using WHERE#

Suppose the business team wants customers from Bengaluru.

SQL
SELECT customer_name, city, total_spent FROM customers WHERE city = 'Bengaluru';

Result:

customer_namecitytotal_spent
AaravBengaluru12500.00
KabirBengaluru15600.00

You can also filter using numeric comparisons:

SQL
SELECT customer_name, total_spent FROM customers WHERE total_spent > 10000;

Result:

customer_nametotal_spent
Aarav12500.00
Kabir15600.00

4. Sort records using ORDER BY#

Suppose you want to see the highest-spending customers first.

SQL
SELECT customer_name, total_spent FROM customers ORDER BY total_spent DESC;

Result:

customer_nametotal_spent
Kabir15600.00
Aarav12500.00
Ishaan9800.00
Diya8200.50
Meera4300.00

Use ASC for ascending order and DESC for descending order. Without ORDER BY, SQL does not guarantee the order of returned rows.

5. Calculate useful business information#

SQL is not limited to retrieving individual records. It can calculate summaries.

For example, calculate the total amount spent by all customers:

SQL
SELECT SUM(total_spent) AS total_customer_spend FROM customers;

Result:

| total_customer_spend | |---:| | 50400.50 |

Count the customers in the table:

SQL
SELECT COUNT(*) AS customer_count FROM customers;

Result:

| customer_count | |---:| | 5 |

Find average spending by city:

SQL
SELECT city, COUNT(*) AS number_of_customers, AVG(total_spent) AS average_spend FROM customers GROUP BY city ORDER BY average_spend DESC;

This groups customers by city and calculates the customer count and average spending for each city.

6. Change existing data#

If Aarav changes his email address, use UPDATE:

SQL
UPDATE customers SET email = 'aarav.new@example.com' WHERE customer_id = 1;

The WHERE condition is important. Without it, the statement would update every row in the table.

To remove a particular customer record:

SQL
DELETE FROM customers WHERE customer_id = 5;

Run data-changing statements carefully, especially in production. Use transactions and verify the affected rows when appropriate.

7. What problems does SQL solve?#

Business problemHow SQL helps
Store customer informationCREATE TABLE and INSERT
Find customers from a citySELECT and WHERE
Show highest-value customersORDER BY
Calculate revenue or totalsSUM() and GROUP BY
Change customer detailsUPDATE
Remove incorrect recordsDELETE
Connect customers with ordersJOIN
Protect related changesTransactions and constraints

8. SQL vs database management system#

These terms are related, but they are not the same:

  • SQL: the language used to communicate with a relational database.
  • DBMS: the software that stores, retrieves, and manages data.
  • RDBMS: a database management system based on the relational model, typically organizing data into related tables.

Examples of relational database systems include PostgreSQL, MySQL, Microsoft SQL Server, Oracle Database, and SQLite. They all support SQL, but some syntax and features differ.

9. Important points to remember#

  • SQL is primarily a declarative language: you describe the data you want or the change you need, and the database determines how to execute it.
  • Tables organize data into rows and columns.
  • CREATE TABLE defines a table.
  • INSERT adds records.
  • SELECT retrieves records.
  • WHERE filters rows.
  • ORDER BY sorts results.
  • Aggregate functions calculate summaries.
  • UPDATE changes records and DELETE removes records.
  • SQL does not guarantee row order unless you specify ORDER BY.

10. Interview-ready answer#

SQL (Structured Query Language) is used to communicate with relational databases. It helps developers create database structures, insert and retrieve records, filter and aggregate information, update existing data, delete records, and manage transactions. For example, an e-commerce application can use SQL to store customer details, retrieve orders, calculate revenue, and identify its highest-value customers.

Practice exercise#

Using the customers table created above, try writing queries to:

  1. Retrieve all customers from Bengaluru.
  2. Find customers whose spending is greater than ₹9,000.
  3. Display customers in ascending order of spending.
  4. Count the number of customers in each city.
  5. Find the customer with the highest spending.

Try writing the queries before checking the examples in this lesson.

Related questions

Explore other topics:Forward Deployed EngineeringPythonAI System DesignMachine Learning