SQL Basics Tutorial β SELECT, WHERE, INSERT, ORDER BY Explained
Introduction β Why Learn SQL Basics First?
Learning the basics of SQL is the foundation of interacting with relational databases. From retrieving specific records to updating millions of rows, SQL provides a clear, English-like syntax for communicating with your database.
Whether youβre a developer, data analyst, or IT student, mastering SQL basics allows you to:
Query and filter data quickly
Modify data reliably
Analyze datasets using built-in functions
This guide covers the most common SQL queries and clauses that form the building blocks of all database operations.
Topics Covered
| Clause | Description |
|---|---|
| SQL SELECT | Retrieve specific columns or entire tables |
| SQL SELECT DISTINCT | Return only unique (non-duplicate) values |
| SQL WHERE | Filter results based on conditions |
| SQL ORDER BY | Sort query output ascending or descending |
| SQL AND, OR, NOT | Combine and invert filter conditions |
| SQL INSERT INTO | Add new rows into a database |
| SQL NULL Values | Handle missing or undefined values |
| SQL UPDATE | Modify existing records |
| SQL DELETE | Remove records from a table |
| SQL SELECT TOP | Return a limited number of rows |
1. SQL SELECT β Retrieve Data from Tables
SELECT column1, column2
FROM table_name;
Retrieves data from one or more columns.
Use SELECT * to fetch all columns, but avoid it in production queries.
2. SQL SELECT DISTINCT β Eliminate Duplicates
SELECT DISTINCT department
FROM employees;
Returns only unique values for the selected column(s).
Helpful when summarizing non-repetitive data.
3. SQL WHERE β Filter Query Results
SELECT * FROM employees
WHERE department = 'Sales';
Filters rows using a condition.
Works with numbers, strings, NULL values, and logical operators.
4. SQL ORDER BY β Sort Results
SELECT name, salary
FROM employees
ORDER BY salary DESC;
Sorts results by one or more columns.
Default order is ASC; use DESC for descending.
5. SQL AND, OR, NOT β Combine Conditions
SELECT * FROM employees
WHERE department = 'Sales' AND salary > 50000;
Use AND and OR to form compound conditions.
NOT negates the condition.
6. SQL INSERT INTO β Add New Records
INSERT INTO employees (name, department)
VALUES ('John Doe', 'Marketing');
Adds a new row to the table.
Ensure column order matches values.
7. SQL NULL Values β Handle Missing Data
SELECT * FROM employees
WHERE bonus IS NULL;
NULL represents unknown or missing values.
Use IS NULL or IS NOT NULL to filter them.
8. SQL UPDATE β Modify Existing Records
UPDATE employees
SET department = 'Sales'
WHERE id = 3;
Updates data in one or more columns.
Always use WHERE to avoid updating all records.
9. SQL DELETE β Remove Records
DELETE FROM employees
WHERE department = 'HR';
Deletes rows based on a condition.
Without a WHERE, all rows will be deleted!
10. SQL SELECT TOP / LIMIT β Return Top N Results
-- MySQL / PostgreSQL:
SELECT * FROM employees LIMIT 10;
-- SQL Server:
SELECT TOP 10 * FROM employees;
Limits the number of rows returned in the result set.
Use LIMIT in MySQL/PostgreSQL, TOP in SQL Server.
Best Practices for Writing SQL Queries
| Do This | Avoid This |
|---|---|
Use SELECT column_names | Avoid SELECT * in production |
Apply WHERE for filtering | Avoid filtering in application code |
| Use proper indentation | Donβt write unformatted long lines |
| Use aliases for clarity | Avoid ambiguous column names |
Summary β Recap & Next Steps
Mastering SQL basics empowers you to retrieve, filter, sort, and group data with confidence. These core queries are used across dashboards, APIs, and business systems.
Key Takeaways:
- Learn core SQL clauses like
SELECT,WHERE,ORDER BY,GROUP BY,DISTINCT,LIMIT, andINSERT - Use combinations of clauses to form powerful queries
- Format SQL code clearly for better readability and maintenance
Real-World Relevance:
These commands power real-time dashboards, data visualizations, user interfaces, and business intelligence systems.
Next Topic: Learn how to combine multiple tables using SQL Joins.
FAQs β SQL Basics
What is the most basic SQL command?
The SELECT command is the most fundamental SQL statement used to retrieve data.
Whatβs the difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after aggregation.
How can I remove duplicate results in a query?
Use SELECT DISTINCT to return only unique values.
Can I sort and filter data at the same time?
Yes, combine WHERE and ORDER BY in the same query for sorted and filtered output.
Which databases support LIMIT or TOP?
LIMIT is used in MySQL, PostgreSQL, and SQLite.
TOP is used in SQL Server.
Share Now :
