Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

SQL in a Nutshell: A Comprehensive Guide

Introduction to SQL

Structured Query Language (SQL) is a standard language used to manage, query, and manipulate relational databases. It is widely used in web applications, data analytics, and enterprise systems. SQL allows users to create, retrieve, update, and delete data efficiently. This guide provides a comprehensive overview of SQL with practical examples and explanations.


1. Basics of SQL

1.1 SQL Syntax

SQL follows a structured syntax. Below is an example of a basic SQL query:

SELECT column1, column2 FROM table_name WHERE condition;
  • SELECT – Retrieves data from a table
  • FROM – Specifies the table
  • WHERE – Filters records based on a condition

1.2 SQL Data Types

SQL provides several data types for defining table columns:

  • INT – Integer values
  • VARCHAR(n) – Variable-length character string
  • TEXT – Large text values
  • DATE – Date values
  • BOOLEAN – True or false values
  • DECIMAL(p,s) – Fixed precision numbers

1.3 Creating a Database and Tables

To create a database and tables, use the following commands:

CREATE DATABASE company_db;
USE company_db;

CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  position VARCHAR(50),
  salary DECIMAL(10,2),
  hire_date DATE
);

2. SQL Commands

SQL is divided into five major categories:

2.1 Data Query Language (DQL)

  • SELECT – Fetches data from tables
SELECT * FROM employees;
SELECT name, salary FROM employees WHERE position = 'Manager';

2.2 Data Definition Language (DDL)

  • CREATE – Creates tables, views, and schemas
  • ALTER – Modifies existing structures
  • DROP – Deletes tables
ALTER TABLE employees ADD COLUMN department VARCHAR(50);
DROP TABLE employees;

2.3 Data Manipulation Language (DML)

  • INSERT – Adds records
  • UPDATE – Modifies records
  • DELETE – Removes records
INSERT INTO employees (id, name, position, salary, hire_date) 
VALUES (1, 'John Doe', 'Manager', 50000, '2024-01-15');

UPDATE employees SET salary = 60000 WHERE id = 1;

DELETE FROM employees WHERE id = 1;

2.4 Data Control Language (DCL)

  • GRANT – Gives user access rights
  • REVOKE – Removes access rights
GRANT SELECT ON employees TO user1;
REVOKE SELECT ON employees FROM user1;

2.5 Transaction Control Language (TCL)

  • COMMIT – Saves changes
  • ROLLBACK – Reverts changes
  • SAVEPOINT – Sets a rollback point
BEGIN;
UPDATE employees SET salary = 65000 WHERE id = 2;
ROLLBACK;

3. Advanced SQL Concepts

3.1 Joins in SQL

Joins are used to combine rows from multiple tables.

  • INNER JOIN – Returns matching rows
  • LEFT JOIN – Returns all rows from the left table and matching rows from the right
  • RIGHT JOIN – Returns all rows from the right table and matching rows from the left
  • FULL OUTER JOIN – Returns all matching and non-matching rows
SELECT employees.name, departments.department_name 
FROM employees 
INNER JOIN departments ON employees.department_id = departments.id;

3.2 Subqueries and Nested Queries

A subquery is a query inside another query:

SELECT name FROM employees WHERE salary = (SELECT MAX(salary) FROM employees);

3.3 Indexing for Performance

Indexes speed up query execution:

CREATE INDEX idx_salary ON employees (salary);

3.4 Stored Procedures and Functions

Stored procedures allow pre-defined SQL logic to be stored:

CREATE PROCEDURE GetEmployee()
AS
BEGIN
    SELECT * FROM employees;
END;

4. SQL Best Practices

  • Use indexing for large datasets
  • Normalize tables to reduce redundancy
  • Use joins instead of subqueries where possible
  • Optimize queries using EXPLAIN ANALYZE
  • Regularly backup databases

5. SQL Real-World Examples

5.1 Employee Management System

SELECT name, salary FROM employees WHERE salary > 50000 ORDER BY salary DESC;

5.2 E-commerce Order Tracking

SELECT orders.order_id, customers.customer_name, orders.total_amount 
FROM orders
JOIN customers ON orders.customer_id = customers.id;

5.3 Banking Transaction Logs

SELECT account_number, transaction_type, amount, transaction_date 
FROM transactions 
WHERE transaction_date BETWEEN '2024-01-01' AND '2024-12-31';

Conclusion

SQL is a powerful language for managing relational databases efficiently. Mastering SQL allows professionals to extract insights, manipulate data, and optimize performance in database-driven applications. This guide provides an in-depth understanding of SQL concepts with examples to enhance learning.

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x