SQL Notes for Beginners: A Complete Guide to Getting Started

SQL Notes for Beginners

Mukesh Sahu
Spread the love

Structured Query Language (SQL) is the standard language for managing and manipulating relational databases. Whether you’re a data analyst, software developer, or aspiring data engineer, understanding SQL is crucial for interacting with data efficiently. This article will serve as a comprehensive set of SQL notes that you can refer to for learning or revision.

Pdf Notes :-  https://drive.google.com/file/d/1UV6_BEKLAiWXxRhDnjpggaqbhdmoWESX/view?usp=sharing


📌 What is SQL?

SQL stands for Structured Query Language. It is used to communicate with relational databases. SQL lets you create, read, update, and delete data — commonly known as CRUD operations.

Some of the most popular database systems that use SQL are:

  • MySQL

  • PostgreSQL

  • SQLite

  • Oracle

  • Microsoft SQL Server


🔹 Basic SQL Syntax

Here’s an example SQL query to fetch data from a table:

SELECT first_name, last_name
FROM employees
WHERE department = 'Sales';

This statement reads:

  • SELECT the columns first_name and last_name

  • FROM the table called employees

  • WHERE the department is ‘Sales’


🏗️ SQL Data Types

When creating tables, it’s important to define the correct data types for each column:

  • INT: Integer values

  • VARCHAR(n): Variable-length string up to n characters

  • DATE: Date values (YYYY-MM-DD)

  • FLOAT or DECIMAL: Decimal numbers

  • BOOLEAN: True/False values


🛠️ Creating Tables

To store data in a database, you first need to create tables.

CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
enrolled_date DATE
);

✏️ Inserting Data

Once a table is created, you can insert records using INSERT INTO:

INSERT INTO students (student_id, name, age, enrolled_date)
VALUES (1, 'Alice', 22, '2024-08-15');

📤 Retrieving Data

Use the SELECT statement to query data:

SELECT * FROM students;

This selects all columns for all rows in the students table. You can filter results using the WHERE clause:

SELECT name FROM students
WHERE age > 20;

🔄 Updating and Deleting Data

You can modify data using UPDATE:

UPDATE students
SET age = 23
WHERE student_id = 1;

To remove data, use DELETE:

DELETE FROM students
WHERE age < 18;

🔍 SQL Clauses and Keywords

Some commonly used SQL clauses include:

  • ORDER BY: Sorts results (ascending by default)

  • GROUP BY: Groups rows that have the same values

  • HAVING: Filters groups (used with GROUP BY)

  • JOIN: Combines rows from two or more tables

  • LIMIT: Restricts number of rows returned

Example of ORDER BY:

SELECT name, age FROM students
ORDER BY age DESC;

🔗 SQL Joins

Joins are used to fetch data from multiple related tables.

  • INNER JOIN: Returns records with matching values in both tables

  • LEFT JOIN: Returns all records from the left table and matched records from the right

  • RIGHT JOIN: Opposite of LEFT JOIN

  • FULL OUTER JOIN: Returns all records when there is a match in either table

Example:

SELECT students.name, courses.course_name
FROM students
INNER JOIN enrollments ON students.student_id = enrollments.student_id
INNER JOIN courses ON enrollments.course_id = courses.course_id;

📊 Aggregate Functions

SQL supports built-in functions for calculations:

  • COUNT(): Number of rows

  • SUM(): Total value

  • AVG(): Average

  • MIN() / MAX(): Smallest/largest value

Example:

SELECT COUNT(*) FROM students
WHERE age > 20;

🧩 Subqueries and Aliases

A subquery is a query nested inside another:

SELECT name FROM students
WHERE age = (SELECT MAX(age) FROM students);

Aliases make column/table names more readable:

SELECT s.name AS student_name
FROM students AS s;

🧼 Best Practices

  • Always back up data before performing DELETE or UPDATE

  • Use WHERE to limit scope of changes

  • Name tables and columns clearly

  • Indent and format your queries for readability


🚀 Conclusion

SQL is a powerful and essential tool for anyone working with data. Whether you’re writing simple queries or complex joins, understanding SQL helps you unlock the full potential of relational databases. Practice is key, so try out queries on sample datasets or use online platforms like  or Mode SQL Tutorial to improve your skills.

Keep this SQL notes guide handy as your quick reference, and happy querying!

Share This Article
Follow:
I am a Software Engineer and the Founder of mcaEducation4all.
Leave a comment

Leave a Reply

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