π 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 :
