Sitemap
Maheswaran Anushan

Maheswaran Anushan

Software Engineer

Learning SQL

4 min readFeb 14, 2025

--

Press enter or click to view image in full size

Introduction to SQL

SQL (Structured Query Language) is the standard language used to interact with relational databases. It allows users to create, read, update, and delete data efficiently.

What is SQL?

SQL is a domain-specific language used for managing and querying data stored in relational database management systems (RDBMS). It provides a standardized way to interact with databases, making it essential for handling structured data

Purpose of SQL

  • Data retrieval and manipulation
  • Defining and managing database structures
  • Ensuring data integrity and security
  • Performing complex queries and aggregations

Relational vs. Non-Relational Databases

Press enter or click to view image in full size
  • Relational Databases (RDBMS): Store data in structured tables with predefined schemas. Examples include MySQL, SQL Server, PostgreSQL, and Oracle.
  • Non-Relational Databases (NoSQL): Store data in flexible formats like JSON, key-value pairs, or graph structures. Examples include MongoDB, Cassandra, and Redis.

1. Getting Started with SQL

1.1 Installing SQL Server

  • Download and install SQL Server from Microsoft’s official website.
  • Install SQL Server Management Studio (SSMS) for database management.
  • Create a sample database for practice.

1.2 Creating a Database

To create a new database, use the following SQL command:

CREATE DATABASE StudentDB;

USE StudentDB;

1.3 Creating Tables

Once a database is created, you can define tables within it:

CREATE TABLE Students (

StudentID INT PRIMARY KEY,

Name VARCHAR(100),

Age INT,

Course VARCHAR(50)

);

1.2 Basic SQL Syntax (Sample Code)

2. SQL Data Types

2.1 Common Data Types in SQL Server

  • INT: Integer values.
  • VARCHAR(n): Variable-length character data.
  • TEXT: Large text data.
  • DATE/TIME: Date and time values.
  • DECIMAL(p, s): Precision decimal numbers.
  • BIT: Boolean values (0 or 1).

3. SQL Queries and Commands

3.1 SELECT Statement

Retrieving data from a table:

SELECT * FROM Students;

Get Maheswaran Anushan’s stories in your inbox

Join Medium for free to get updates from this writer.

SELECT Name, Age FROM Students WHERE Age > 18;

3.2 Filtering Data (WHERE Clause)

SELECT * FROM Students WHERE Age >= 20 AND Course = ‘Computer Science’;

3.3 Sorting Data (ORDER BY Clause)

SELECT * FROM Students ORDER BY Age DESC;

3.4 Grouping Data (GROUP BY Clause)

SELECT Course, COUNT(*) FROM Students GROUP BY Course;

4. SQL Joins

SQL joins are used to combine rows from two or more tables based on a related column.

4.1 INNER JOIN

SELECT Students.Name, Courses.CourseName

FROM Students

INNER JOIN Courses ON Students.CourseID = Courses.CourseID;

4.2 LEFT JOIN

SELECT Students.Name, Courses.CourseName

FROM Students

LEFT JOIN Courses ON Students.CourseID = Courses.CourseID;

4.3 RIGHT JOIN

SELECT Students.Name, Courses.CourseName

FROM Students

RIGHT JOIN Courses ON Students.CourseID = Courses.CourseID;

4.4 FULL OUTER JOIN

SELECT Students.Name, Courses.CourseName

FROM Students

FULL OUTER JOIN Courses ON Students.CourseID = Courses.CourseID;

5. Advanced SQL Concepts

5.1 Subqueries

SELECT Name FROM Students WHERE CourseID IN (SELECT CourseID FROM Courses WHERE CourseName = ‘Math’);

5.2 Stored Procedures

CREATE PROCEDURE GetStudents

AS

BEGIN

SELECT * FROM Students;

END;

5.3 Indexing

CREATE INDEX idx_student_name ON Students (Name);

6. Database Administration

6.1 Creating and Managing Users

CREATE LOGIN User1 WITH PASSWORD = ‘Password123’;

CREATE USER User1 FOR LOGIN User1;

6.2 Backups and Restores

BACKUP DATABASE StudentDB TO DISK = ‘C:\Backup\StudentDB.bak’;

RESTORE DATABASE StudentDB FROM DISK = ‘C:\Backup\StudentDB.bak’;

7. Best Practices

  • Use indexing wisely for performance optimization.
  • Normalize database tables to reduce redundancy.
  • Use transactions to maintain data integrity.
  • Follow security best practices to protect data.

Conclusion

This guide provides a structured approach to learning SQL. Practicing SQL queries and understanding database management will help in mastering the language for real-world applications.

4.2 LEFT JOIN

--

--