SQL Tutorial
Estimated reading: 4 minutes 506 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 :
Share

πŸ“„ SQL Basics – Core Queries & Clauses

Or Copy Link

CONTENTS
Scroll to Top