SQL Tutorial
Estimated reading: 4 minutes 32 views

πŸ“„ 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 SELECTRetrieve specific columns or entire tables
πŸ” SQL SELECT DISTINCTReturn only unique (non-duplicate) values
πŸ“Œ SQL WHEREFilter results based on conditions
πŸ“Š SQL ORDER BYSort query output ascending or descending
🧠 SQL AND, OR, NOTCombine and invert filter conditions
✍️ SQL INSERT INTOAdd new rows into a database
🧺 SQL NULL ValuesHandle missing or undefined values
πŸ› οΈ SQL UPDATEModify existing records
🧹 SQL DELETERemove records from a table
πŸ” SQL SELECT TOPReturn 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_namesAvoid SELECT * in production
Apply WHERE for filteringAvoid filtering in application code
Use proper indentationDon’t write unformatted long lines
Use aliases for clarityAvoid 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, and INSERT
  • 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ“„ SQL Basics – Core Queries & Clauses

Or Copy Link

CONTENTS
Scroll to Top