0% found this document useful (0 votes)
16 views2 pages

Complete SQL Notes with Examples

Uploaded by

ladeprashanth4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Complete SQL Notes with Examples

Uploaded by

ladeprashanth4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Complete SQL Notes with Real-time Examples

1. What is SQL?

SQL (Structured Query Language) is used to manage and manipulate relational databases. It is used in

real-time applications like Amazon, Flipkart, and banking apps to manage customer data, orders,

transactions, etc.

2. Categories of SQL

DDL - Define DB structure: CREATE, ALTER, DROP, TRUNCATE

DML - Work with data: SELECT, INSERT, UPDATE, DELETE

DCL - Access control: GRANT, REVOKE

TCL - Transactions: COMMIT, ROLLBACK, SAVEPOINT

3. DDL Commands

CREATE TABLE Users (ID INT, Name VARCHAR(100));

ALTER TABLE Users ADD Email VARCHAR(100);

DROP TABLE Users;

TRUNCATE TABLE Users;

Real-time: Used by Amazon to define and modify user or product tables.

4. DML Commands

SELECT * FROM Users;

INSERT INTO Users VALUES (1, 'Akhil');

UPDATE Users SET Name='Sai' WHERE ID=1;

DELETE FROM Users WHERE ID=1;

Real-time: Flipkart uses DML to show product listings or update cart details.

5. DCL & TCL

GRANT SELECT ON Users TO analyst;

REVOKE SELECT ON Users FROM analyst;

COMMIT, ROLLBACK help manage transaction consistency in banking apps.

6. SQL Clauses

Page 1
Complete SQL Notes with Real-time Examples

WHERE, ORDER BY, GROUP BY, HAVING, IN, BETWEEN, LIKE, DISTINCT

Real-time: Amazon uses WHERE and ORDER BY to filter and sort products by price.

7. SQL Joins

INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN, SELF JOIN

Real-time: Used to link user orders with payment details in e-commerce apps.

8. Subqueries

SELECT Name FROM Users WHERE Age > (SELECT AVG(Age) FROM Users);

9. Views & Functions

CREATE VIEW TeenUsers AS SELECT * FROM Users WHERE Age < 20;

Functions: COUNT(), SUM(), AVG(), UPPER(), NOW(), ROUND()

Real-time: Reports in admin dashboards use views and functions.

10. Indexes & Constraints

CREATE INDEX idx_name ON Users(Name);

Constraints: PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE, CHECK, DEFAULT

Real-time: Ensures product IDs are unique and not null in Amazon database.

11. Interview Questions

- Difference between WHERE and HAVING

- DELETE vs TRUNCATE vs DROP

- What are ACID properties?

- INNER JOIN vs LEFT JOIN

- What is Normalization?

- UNION vs UNION ALL

Page 2

Common questions

Powered by AI

SQL functions and views provide a powerful abstraction layer for handling complex data operations and enhancing database management efficiency. Functions like COUNT(), SUM(), AVG(), and NOW() are used to perform calculations on data, facilitating analytical tasks such as generating sales reports or calculating average inventory levels. Views are virtual tables created with a specific query and can simplify complex queries by providing a consistent data subset view, such as showing only teen users with a view named TeenUsers. They streamline repeated access to commonly requested data, reduce redundancy, and enhance security by allowing users to query specific parts of the data without exposing the entire dataset. In practice, using views and functions is crucial for applications requiring regular reporting and dashboard creation .

DCL (Data Control Language) commands, such as GRANT and REVOKE, are focused on controlling access to database objects, ensuring that only authorized users can modify or retrieve data, essential for security and privacy in applications where sensitive data is managed. In contrast, TCL (Transaction Control Language) commands like COMMIT, ROLLBACK, and SAVEPOINT are used to manage and ensure the consistency and integrity of transactions within the database, handling multiple operations as a single unit of work and critical in environments where transaction accuracy is vital, such as in banking apps where consistent financial data is necessary .

DELETE, TRUNCATE, and DROP commands in SQL serve distinct purposes and have varying impacts on a database. DELETE removes specific rows from a table based on a condition, retaining the table's structure and applicable where row-specific operations or conditions are involved. TRUNCATE deletes all rows from a table quickly but retains the table schema, suitable for resetting table data without structural changes. DROP eliminates the table entirely, including metadata, rendering it unusable. These differences influence performance, with TRUNCATE generally faster for bulk deletions without logging each row's removal, whereas DELETE is logged per row and retains integrity features like queries on individual transaction effects. The choice among them depends on the need for data retention and subsequent data usage requirements .

SQL clauses such as WHERE, ORDER BY, GROUP BY, HAVING, IN, BETWEEN, LIKE, and DISTINCT are vital for refining data retrieval and enhancing query efficiency. WHERE clause filters records based on conditions, critical for narrowing down search results in large datasets, like Amazon filtering products by category or price. ORDER BY sorts results, enhancing user experience by displaying products by popularity or price. GROUP BY and HAVING aggregate data to produce meaningful summaries, important in reporting. LIKE and IN simplify pattern matching and value sets, aiding in flexible searches. These clauses optimize performance and usability in real-time applications by ensuring data accuracy, speed, and relevance .

SQL joins are essential for combining data from multiple tables, enabling complex queries that are crucial in e-commerce and other applications for linking related information. For instance, INNER JOIN is used to retrieve records with matching values in both tables, useful in matching customer orders with payment details. LEFT JOIN retrieves all records from the left table and matched records from the right, which helps in listing all customers including those without orders. FULL JOIN returns records when there is a match in one of the tables, helping in comprehensive data analysis of transactions and customers even if some details are missing. Real-time applications like e-commerce platforms regularly use these joins to integrate and report data efficiently .

Subqueries play a pivotal role in SQL by enabling complex queries through nested operations within a primary query. They allow developers to dynamically calculate values or conditionally filter results based on separate datasets, essentially functioning as a query within a query. For instance, selecting users older than the average age by incorporating a subquery to calculate the average age demonstrates their practical usage. This enhances query functionality by allowing precise and flexible data retrieval that can adapt to varying data conditions, thus optimizing data analysis and application functionality .

Indexes and constraints are critical for enhancing the performance and integrity of databases. Indexes, such as those created with CREATE INDEX on columns like Name in a Users table, optimize data retrieval speeds, significantly improving query performance. Constraints like PRIMARY KEY ensure each record's uniqueness and quick identification, while FOREIGN KEY constraints maintain relational integrity between related tables. NOT NULL and UNIQUE constraints enforce data validity and prevent duplicate entries. For example, Amazon ensures that product IDs are unique and not null, maintaining data accuracy and consistency during transactions and user interactions .

Common SQL interview questions include topics like the difference between WHERE and HAVING clauses, DELETE vs. TRUNCATE vs. DROP, inner workings of ACID properties, INNER JOIN vs. LEFT JOIN distinctions, concepts of Normalization, and UNION vs. UNION ALL differences. These questions are significant as they cover essential SQL operations, query optimization, transaction management, and data integrity principles, crucial for a strong foundational understanding of SQL and its real-world applications in database management and development .

DDL (Data Definition Language) commands such as CREATE, ALTER, DROP, and TRUNCATE are primarily used to define, modify, and manage the structure of database objects like tables and indexes. For example, Amazon might use these to define new user or product tables. In contrast, DML (Data Manipulation Language) commands, including SELECT, INSERT, UPDATE, and DELETE, are used to manage and manipulate the data within these structures, such as Flipkart displaying product information or updating customer orders. DDL affects the schema and structure, while DML deals directly with data within these structures .

SQL commands are categorized into four main types: DDL (Data Definition Language), DML (Data Manipulation Language), DCL (Data Control Language), and TCL (Transaction Control Language). DDL includes commands like CREATE, ALTER, and DROP, used to define and modify database structures, as seen in user or product table modifications by Amazon. DML comprises SELECT, INSERT, UPDATE, and DELETE to work with data, such as Flipkart displaying product listings or updating cart details. DCL involves GRANT and REVOKE for access control, managing permissions like analytics access to user tables. TCL commands like COMMIT and ROLLBACK ensure transaction consistency, crucial for applications like banking apps to maintain data integrity during transactions .

You might also like