Introduction

When developing full-stack applications, understanding SQL and how to write queries is essential.

SQL (Structured Query Language) allows you to store, retrieve, and manipulate data in a relational database like SQL Server, MySQL, or PostgreSQL.

Even if you are mainly working on Angular, React, or ASP.NET Core, knowing SQL basics is critical for building functional applications.

This guide will explain SQL concepts, queries, and examples you can use in full-stack projects.

What is SQL?

SQL is a language used to communicate with relational databases.
It allows you to perform operations like:

Common relational databases used in full-stack projects include:

Basic SQL Operations

SQL commands are usually categorized as:

  1. DDL (Data Definition Language): Defines database structure.

  2. DML (Data Manipulation Language): Manages data inside tables.

  3. DCL (Data Control Language): Manages user permissions.

  4. TCL (Transaction Control Language): Manages database transactions.

For full-stack projects, you will mostly use DDL and DML.

Step 1: Creating a Database and Tables

Create Database

CREATE DATABASE FullStackDB;

This command creates a new database named FullStackDB.

Use the Database

USE FullStackDB;

Create Table

Example: Create a Users table

CREATE TABLE Users (
    Id INT PRIMARY KEY IDENTITY(1,1),
    Name NVARCHAR(100) NOT NULL,
    Email NVARCHAR(100) NOT NULL,
    CreatedAt DATETIME DEFAULT GETDATE()
);

Step 2: Inserting Data

To add data to the Users table:

INSERT INTO Users (Name, Email)
VALUES ('Rahul Sharma', '[email protected]');

INSERT INTO Users (Name, Email)
VALUES ('Priya Singh', '[email protected]');

Step 3: Querying Data

Select All Data

SELECT * FROM Users;

Select Specific Columns

SELECT Name, Email FROM Users;

Filter Data Using WHERE

SELECT * FROM Users
WHERE Name = 'Rahul Sharma';

Step 4: Updating Data

UPDATE Users
SET Email = '[email protected]'
WHERE Name = 'Rahul Sharma';

Step 5: Deleting Data

DELETE FROM Users
WHERE Name = 'Priya Singh';

Step 6: Sorting Data

SELECT * FROM Users
ORDER BY Name ASC;

Step 7: Using Joins

In full-stack projects, tables are often related.

Example: Users and Orders

CREATE TABLE Orders (
    Id INT PRIMARY KEY IDENTITY(1,1),
    UserId INT,
    Product NVARCHAR(100),
    Quantity INT,
    FOREIGN KEY (UserId) REFERENCES Users(Id)
);

Inner Join

SELECT Users.Name, Orders.Product, Orders.Quantity
FROM Users
INNER JOIN Orders ON Users.Id = Orders.UserId;

Left Join

SELECT Users.Name, Orders.Product, Orders.Quantity
FROM Users
LEFT JOIN Orders ON Users.Id = Orders.UserId;

Step 8: Aggregation Functions

SQL has built-in functions for counting, summing, and averaging:

-- Count total users
SELECT COUNT(*) AS TotalUsers FROM Users;

-- Sum quantity of all orders
SELECT SUM(Quantity) AS TotalQuantity FROM Orders;

-- Average quantity per order
SELECT AVG(Quantity) AS AverageQuantity FROM Orders;

Step 9: Filtering with LIKE

SELECT * FROM Users
WHERE Email LIKE '%example.com';

Step 10: Using Transactions

Transactions help ensure data integrity:

BEGIN TRANSACTION;

UPDATE Users SET Email = '[email protected]' WHERE Id = 1;
INSERT INTO Orders (UserId, Product, Quantity) VALUES (1, 'Laptop', 2);

COMMIT;  -- Save changes
-- ROLLBACK;  -- Undo changes if there is an error

Step 11: Best Practices for Full-Stack Projects

  1. Use Primary Keys: Every table should have a unique identifier.

  2. Use Foreign Keys: Maintain relationships between tables.

  3. Avoid Hardcoding Data: Use parameters in queries for safety.

  4. Normalize Data: Reduce redundancy by splitting related data into tables.

  5. Use Indexes: Improve query performance on large tables.

  6. Always Test Queries: Check with sample data before integrating with backend.

Step 12: Connecting SQL with Full-Stack Applications

Example workflow

Angular Frontend → HTTP Request → ASP.NET Core API → SQL Server → API Response → Angular

Conclusion

Learning SQL basics is essential for full-stack developers.

With this knowledge, you can confidently connect your frontend frameworks like Angular or React to a SQL database via backend APIs and build robust full-stack applications.