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
- 1. Understand the data with a practical example
- Create a table
- Insert sample records
- 2. Retrieve data using SELECT
- 3. Filter records using WHERE
- 4. Sort records using ORDER BY
- 5. Calculate useful business information
- 6. Change existing data
- 7. What problems does SQL solve?
- 8. SQL vs database management system
- 9. Important points to remember
- 10. Interview-ready answer
- Practice exercise
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.
SQLCREATE 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.
SQLINSERT 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_id | customer_name | city | total_spent | |
|---|---|---|---|---|
| 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 |
2. Retrieve data using SELECT#
Suppose you want to see all customers.
SQLSELECT * FROM customers;
To retrieve only customer names and cities:
SQLSELECT customer_name, city FROM customers;
The result is:
| customer_name | city |
|---|---|
| Aarav | Bengaluru |
| Diya | Mumbai |
| Kabir | Bengaluru |
| Meera | Delhi |
| Ishaan | Pune |
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.
SQLSELECT customer_name, city, total_spent FROM customers WHERE city = 'Bengaluru';
Result:
| customer_name | city | total_spent |
|---|---|---|
| Aarav | Bengaluru | 12500.00 |
| Kabir | Bengaluru | 15600.00 |
You can also filter using numeric comparisons:
SQLSELECT customer_name, total_spent FROM customers WHERE total_spent > 10000;
Result:
| customer_name | total_spent |
|---|---|
| Aarav | 12500.00 |
| Kabir | 15600.00 |
4. Sort records using ORDER BY#
Suppose you want to see the highest-spending customers first.
SQLSELECT customer_name, total_spent FROM customers ORDER BY total_spent DESC;
Result:
| customer_name | total_spent |
|---|---|
| Kabir | 15600.00 |
| Aarav | 12500.00 |
| Ishaan | 9800.00 |
| Diya | 8200.50 |
| Meera | 4300.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:
SQLSELECT SUM(total_spent) AS total_customer_spend FROM customers;
Result:
| total_customer_spend | |---:| | 50400.50 |
Count the customers in the table:
SQLSELECT COUNT(*) AS customer_count FROM customers;
Result:
| customer_count | |---:| | 5 |
Find average spending by city:
SQLSELECT 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:
SQLUPDATE 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:
SQLDELETE 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 problem | How SQL helps |
|---|---|
| Store customer information | CREATE TABLE and INSERT |
| Find customers from a city | SELECT and WHERE |
| Show highest-value customers | ORDER BY |
| Calculate revenue or totals | SUM() and GROUP BY |
| Change customer details | UPDATE |
| Remove incorrect records | DELETE |
| Connect customers with orders | JOIN |
| Protect related changes | Transactions 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 TABLEdefines a table.INSERTadds records.SELECTretrieves records.WHEREfilters rows.ORDER BYsorts results.- Aggregate functions calculate summaries.
UPDATEchanges records andDELETEremoves 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:
- Retrieve all customers from Bengaluru.
- Find customers whose spending is greater than ₹9,000.
- Display customers in ascending order of spending.
- Count the number of customers in each city.
- Find the customer with the highest spending.
Try writing the queries before checking the examples in this lesson.
References
- 1.PostgreSQL Tutorial — The SQL Languagepostgresql.org
- 2.PostgreSQL — Table Basicspostgresql.org
- 3.PostgreSQL — SQL Language Referencepostgresql.org
Related questions
Explore other topics:Forward Deployed EngineeringPythonAI System DesignMachine Learning