SQL Fundamentals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

SQL Fundamentals A

Beginner's Guide
SQL Fundamentals: A Beginner's Guide

Introduction to SQL

SQL (Structured Query Language) is a powerful tool used for managing and
manipulating relational databases. It provides a standardized way to interact with
databases, enabling users to perform various operations such as querying, updating,
and managing data. SQL is essential for anyone working with databases, including
developers, data analysts, and database administrators.

Basic Concepts

Relational Databases: SQL is primarily used with relational databases, which organize
data into tables consisting of rows and columns. Each column represents an attribute
of the data, while each row represents an individual record.

SQL Statements: SQL consists of various types of statements used to perform different
tasks. These include Data Definition Language (DDL), Data Manipulation Language
(DML), Data Control Language (DCL), and Transaction Control Language (TCL).

Data Definition Language (DDL)

CREATE TABLE Statement: Allows users to create a new table in the database by
specifying the table's structure, including column names, data types, and constraints.

CREATE TABLE employees (


id INT PRIMARY KEY,
name VARCHAR(100),
age INT,
salary DECIMAL(10, 2)
);

ALTER TABLE Statement: Modifies an existing table by adding, modifying, or dropping


columns.
ALTER TABLE employees
ADD COLUMN department VARCHAR(100);

DROP TABLE Statement: Deletes a table and all its data from the database.

DROP TABLE employees;

Data Manipulation Language (DML)

SELECT Statement: Retrieves data from one or more tables based on specified criteria.

SELECT * FROM employees;

INSERT Statement: Adds new records to a table.

INSERT INTO employees (id, name, age, salary) VALUES (1, 'John Doe', 30, 50000);

UPDATE Statement: Modifies existing records in a table.

UPDATE employees SET salary = 55000 WHERE id = 1;

DELETE Statement: Deletes records from a table based on specified conditions.

DELETE FROM employees WHERE id = 1;


Filtering and Sorting Data

WHERE Clause: Filters rows based on specified conditions.

SELECT * FROM employees WHERE age > 30;

ORDER BY Clause: Sorts the result set in ascending or descending order.

SELECT * FROM employees ORDER BY salary DESC;

GROUP BY Clause: Groups rows that have the same values into summary rows.

SELECT department, COUNT(*) FROM employees GROUP BY department;

Advanced Queries -Part 1

JOINs: Combine rows from two or more tables based on a related column.

SELECT * FROM employees e JOIN departments d ON e.department_id = d.id;

INNER JOIN: Retrieves rows where there is a match in both tables.


SELECT * FROM employees e INNER JOIN departments d ON e.department_id = d.id;

Advanced Queries -Part 2

LEFT JOIN: Retrieves all rows from the left table and matching rows from the right table.

SELECT * FROM employees e LEFT JOIN departments d ON e.department_id = d.id;

RIGHT JOIN: Retrieves all rows from the right table and matching rows from the left
table.

SELECT * FROM employees e RIGHT JOIN departments d ON e.department_id = d.id;

FULL JOIN: Retrieves all rows when there is a match in either table.

SELECT * FROM employees e FULL JOIN departments d ON e.department_id = d.id;

Subqueries: Nested queries used within another query to perform advanced filtering or
data retrieval.

SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments WHERE


name = 'IT');

Aggregate Functions and Grouping

Aggregate Functions: Perform calculations on a set of values and return a single value.
SELECT COUNT(*), AVG(salary) FROM employees;

GROUP BY Clause: Groups rows based on specified columns.

SELECT department, AVG(salary) FROM employees GROUP BY department;

Data Control Language (DCL)

GRANT Statement: Grants specific permissions to users.

GRANT SELECT, INSERT ON employees TO user1;

REVOKE Statement: Revokes previously granted permissions.

REVOKE INSERT ON employees FROM user1;

Transaction Control Language (TCL)

COMMIT Statement: Saves changes made during the current transaction.

COMMIT;

ROLLBACK Statement: Reverts changes made during the current transaction.


ROLLBACK;

Indexes

Creating Indexes: Improves search performance by creating indexes on columns used


frequently in queries.

CREATE INDEX idx_name ON employees (name);

Views

Creating Views: Virtual tables derived from the result set of a SELECT query, simplifying
complex queries and securing data access.

CREATE VIEW employee_view AS SELECT * FROM employees WHERE department = 'IT';

Advanced Topics

Stored Procedures: Precompiled SQL code stored in the database and executed when
called.

CREATE PROCEDURE getEmployee


AS BEGIN SELECT * FROM
employees;

END;
Triggers: Special type of stored procedure that automatically executes when certain
events occur in the database.

CREATE TRIGGER update_salary


AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
-- Trigger logic goes here
END;

Thank You

Thank you for taking the time to learn SQL


fundamentals! If you have any questions or need
further assistance, don't hesitate to reach out. Happy
querying!

Feel free to follow me on


[LinkedIn](www.linkedin.com/in/kamesh21).

You might also like