Skip to content

arjunkumar0304/SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 

Repository files navigation

SQL

1. Relational Database Basics:

It is a type of database that stores data in tables — just like an Excel sheet. Each table has rows (data records) and columns (data fields).

  • Example: A table called Students might have columns like ID, Name, and Age, and rows with actual student details.

Relation: Different tables can be linked using a key (like a student’s ID can link to their marks in another table).

2. Database Management System (DBMS)

A DBMS is a software that helps us store, manage, and use data easily. It allows users to add, update, delete, and fetch data from databases.

Examples:

  • MySQL
  • PostgreSQL
  • SQL Server

These tools help us write and run SQL queries.

3. Basic SQL Syntax

SQL (Structured Query Language) is used to talk to the database. We use it to:

Get data: SELECT * FROM students;

Add data: INSERT INTO students (name, age) VALUES ('Arjun', 22);

Update data: UPDATE students SET age = 23 WHERE name = 'Arjun';

Delete data: DELETE FROM students WHERE name = 'Arjun';

4. DDL (Data Definition Language)

DDL commands help manage the structure of the database (not the data itself). Main commands: CREATE, ALTER, DROP, and TRUNCATE.

4.1 CREATE

Used to create new database objects such as tables or indexes. Screenshot 2025-10-22 183443 Screenshot 2025-10-22 184339

  • PRIMARY KEY → unique identifier for each row.
  • FOREIGN KEY → link between two tables.
  • CHECK → ensure data follows certain rules.

4.2 ALTER

Used to change the structure of an existing table. Screenshot 2025-10-22 184609

4.3 DROP

Used to delete database objects permanently.

4.4 TRUNCATE

Used to delete all rows from a table, but keep the table structure.

5. DML (Data Manipulation Language)

DML commands are used to work with the data stored in tables — inserting, updating, deleting, or reading it.

5.1 INSERT

Used to add new records to a table. Screenshot 2025-10-22 185431

5.2 UPDATE

Used to change existing records. Screenshot 2025-10-22 185835

5.3 DELETE

Used to remove specific rows from a table. Screenshot 2025-10-22 190142

5.4 SELECT

Used to fetch data from tables.

Screenshot 2025-10-22 185444

6. DCL (Data Control Language)

Used to control access or permissions to the data in a database. It helps in granting and revoking privileges to users on database objects (like tables, views, etc.).

EXAMPLE:

GRANT

Used to give permissions to a user.

( SYNTAX : GRANT privilege_name ON object_name TO user_name; )

Example for student database

  • (GRANT SELECT, INSERT ON student TO teacher1;)
  • This allows teacher1 to view (SELECT) and add (INSERT) records in the student table.

REVOKE

Used to take back permissions that were previously granted.

( Syntax: REVOKE privilege_name ON object_name FROM user_name;)

Example for student Database

  • (REVOKE INSERT ON student FROM teacher1;)
  • This removes teacher1’s permission to insert data into the student table.

7. DQL (Data Query Language)

Used to fetch or retrieve data from the database.

GROUP BY

Used to group rows that have the same value in one or more columns. Usually used with aggregate functions like COUNT, SUM, AVG, etc. Screenshot 2025-10-23 161651

HAVING

is like WHERE, but it is used after GROUP BY to filter groups. Screenshot 2025-10-23 161803

ORDER BY

used to sort the result — either ascending (ASC) or descending (DESC) order Screenshot 2025-10-23 161550

8. TCL (Transaction Control Language)

TCL commands are used to manage transactions in a database. A transaction is a group of SQL operations that are treated as a single unit of work — either all succeed or all fail.

  • COMMIT-- permanently saves changes,
  • ROLLBACK -- undoes uncommitted changes,
  • SAVEPOINT -- lets you set a point to roll back to within a transaction.\

9.Constraints

  • rules you set on your table to keep your data correct and safe.
  • They make sure no wrong or duplicate data goes inside your table.

9.1 PRIMARY KEY

  • Uniquely identifies each record in a table.
  • It cannot be NULL and must be unique. Example: studentID INT PRIMARY KEY
Screenshot 2025-10-24 190624

9.2 FOREIGN KEY

  • Creates a link between two tables.
  • It ensures the value in one table matches a value in another table. Example: FOREIGN KEY (deptID) REFERENCES department(deptID)
Screenshot 2025-10-24 190930

9.3 UNIQUE

Makes sure all values in a column are different (no duplicates). Example: email VARCHAR(30) UNIQUE

9.4 CHECK

Ensures that the values in a column meet a specific condition. Example: CHECK (age >= 18)

9.5 NOT NULL

Ensures a column cannot have a NULL (empty) value. Example: name VARCHAR(20) NOT NULL

10. Joins:

  • INNER JOIN: rows that match in both tables.
  • LEFT JOIN: all rows from the left table, plus matching rows from the right (or NULL when no match).
  • RIGHT JOIN: all rows from the right table, plus matching rows from the left (or NULL when no match).
  • FULL JOIN: all rows from both tables; where no match exists, columns show NULL.
  • CROSS JOIN: every row from left paired with every row from right (Cartesian product).

INNER JOIN

Screenshot 2025-10-24 190955

LEFT JOIN

Screenshot 2025-10-24 191134

RIGHT JOIN:

Screenshot 2025-10-24 191154

FULL JOIN:

Screenshot 2025-10-24 191223

CROSS JOIN

Screenshot 2025-10-24 191240

Aggregation functions

  • COUNT(*) — count rows
Screenshot 2025-10-24 193142
  • SUM(column) — add values in a column
Screenshot 2025-10-24 193509
  • AVG(column) — average value
Screenshot 2025-10-24 193202
  • MIN(column) and MAX(column) — smallest / largest
Screenshot 2025-10-24 193229

SQL Clause:

  • SELECT — choose columns / expressions

  • FROM — table(s)

  • WHERE — filter rows before grouping

Screenshot 2025-10-24 193832
  • GROUP BY — form groups of rows
Screenshot 2025-10-24 194011
  • HAVING — filter groups after grouping
Screenshot 2025-10-24 194044
  • ORDER BY — sort the final result
Screenshot 2025-10-24 194100

12.1 Arithmetic Operators: +, -, , /

Add

Screenshot 2025-10-24 194754

Sub

Screenshot 2025-10-24 194809

Mul

Screenshot 2025-10-24 194833

div

Screenshot 2025-10-24 194846

12.2 Comparison Operators

Screenshot 2025-10-24 195337 Screenshot 2025-10-24 200758

12.3 Logical Operators

Screenshot 2025-10-24 195354 Screenshot 2025-10-24 201018

12.4 String Concatenation Operator: ||

Screenshot 2025-10-24 201252

13.Procedures:

Procedure in SQL is like a saved program or a set of SQL commands that you can reuse anytime.

13.1 CREATE PROCEDURE: Creating stored procedures.

Screenshot 2025-10-27 130955

13.2 ALTER PROCEDURE: Modifying existing stored procedures.

Screenshot 2025-10-27 131155

13.3 DROP PROCEDURE: Deleting stored procedures.

13.4 EXECUTE: Running a stored procedure.

Screenshot 2025-10-27 131118

14. Subquery

A Subquery is a query inside another query. It helps to get data from one query and use it inside another.

Find the oldest student Screenshot 2025-10-27 175807

Find youngest student name Screenshot 2025-10-27 181521

14.2 CTE

CTE means Common Table Expression. It is like a temporary table that you create inside a query — only for that query.

Screenshot 2025-10-27 184853

15. Views:

A view is a virtual table. It’s used to see specific data easily without writing long queries again and again. We use CREATE VIEW to make it and DROP VIEW to remove it.”

3.1 CREATE VIEW: Creating views for simplified querying.

Screenshot 2025-10-27 145458

3.2 DROP VIEW: Removing views.

Screenshot 2025-10-27 145652

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published