Skip to main content

SQL - The Language of Data

SQL (Structured Query Language) is the universal language used to communicate with Relational Databases. Whether you use MySQL, PostgreSQL, or SQL Server, the way you ask for data is 95% the same.

The 4 Pillars: CRUD Operations

In backend development, almost everything you do with a database falls into one of four categories: CRUD.

Use the INSERT command to add new records to a table.

INSERT INTO Students (name, email, age) 
VALUES ('Aryan Kumar', 'aryan@codeharborhub.github.io', 21);

Joining the Dots: SQL JOINS

The true power of SQL is its ability to combine data from multiple tables. This is called a JOIN.

Imagine you have a Students table and a Courses table. To see which student is in which course, you link them:

SELECT Students.name, Courses.course_name
FROM Students
JOIN Enrollments ON Students.id = Enrollments.student_id
JOIN Courses ON Enrollments.course_id = Courses.id;

Filtering and Sorting

SQL allows you to organize your data exactly how you want it.

  • ORDER BY: Sort your results (e.g., ORDER BY age DESC).
  • LIMIT: Only get a specific number of rows (great for pagination).
  • LIKE: Search for patterns (e.g., WHERE name LIKE 'A%' finds all names starting with A).

Advanced Data Manipulation

Sometimes you need to perform math or group data together.

-- Count how many students are in the database
SELECT COUNT(*) FROM Students;

-- Find the average age of students
SELECT AVG(age) FROM Students;

Summary Checklist

  • I understand that SQL is the language used to manage RDBMS.
  • I can explain what CRUD stands for.
  • I know the danger of running UPDATE or DELETE without a WHERE clause.
  • I understand how JOIN combines data from different tables.
Professional Warning

In a real production environment, never use SELECT * (Select All) if you only need one or two columns. Selecting only what you need makes your application much faster and saves memory!