Master SQL Effortlessly: Powerful Handwritten Notes for Success

Table of Contents:

Introduction:

Master SQL Effortlessly: Powerful Handwritten Notes for Success :

  • SQL is one of the important thing in the branch of computer science.
  • SQL is one of the fundamental tools in the database management system. It is widely used for handling and manipulating the data.
  • It provides secure access control for different users.
  • If you build any kind of online application, you must need the Database Management System to store and retrieve the data.
  • It plays an pivot role in storing data securely for application like transactions, etc.

Why are handwritten notes helpful for learning SQL ?

It undergoes multiple aspects like

Easy understanding of Syntax

  • It makes it easier for users to understand the content easily and efficiently.
  • In the tech industry, SQL is very essential as the role is as database administrator.
  • It feels better to the user if we compare the computer-generated and the handwritten notes.
  • Handwritten notes have a higher chance of getting remembered easily and somehow feel easier to the user.

Contents Included in SQL:

Introduction to SQL

Database concepts

Datatypes

SQL Commands

Constraints

SQL Clauses

Joins and Relationships

Indexes and Optimization

Subqueries and Nested Queries

Views and Stored Procedures

Transaction & ACID Properties

Triggers

Functions & Operators

Normalisation

Cloud Based SQL Services

1. SQL Basics: Understanding the Fundamentals

  • SQL stands for Structured Query Language.
  • It is used to communicate with and manage relational databases.
  • SQL allows you to perform operations like retrieving, updating, and deleting data.

2. SQL Syntax: Structure and Rules

  • SQL syntax is not case-sensitive, but it’s a good practice to write keywords in uppercase.
  • Every SQL statement ends with a semicolon (;), especially in complex queries.

3. SELECT Statement: Retrieving Data

  • The SELECT statement is used to query data from a database.
  • Basic syntax:SELECT column1, column2 FROM table_name;
  • Use * to select all columns: SELECT * FROM table_name;

4. WHERE Clause: Filtering Data

  • The WHERE WHEREclause filters records based on specific conditions.
  • Syntax:SELECT column1 FROM table_name WHERE condition;
  • Example:SELECT * FROM Employees WHERE age > 30;

5. AND, OR, NOT Operators

  • Combine multiple conditions using AND, OR, and NOT.
  • Example:SELECT * FROM Employees WHERE age > 30 AND department = 'Sales';

6. ORDER BY Clause: Sorting Data

  • ORDER BYSorting query results in ascending (ASC) or descending (DESC) order.
  • Example:SELECT * FROM Employees ORDER BY salary DESC;

7. DISTINCT Keyword: Removing Duplicates

  • Use DISTINCT to remove duplicate values from the results.
  • Example:SELECT DISTINCT city FROM Employees;

8. Aggregate Functions: Summarizing Data

  • Common aggregate functions: COUNT(), SUM(), AVG(), MAX(), MIN().
  • Example:SELECT COUNT(*) FROM Employees;

9. GROUP BY Clause: Grouping Data

  • GROUP BYGroup rows that have the same values into summary rows.
  • Example:SELECT department, COUNT(*) FROM Employees GROUP BY department;

10. HAVING Clause: Filtering Groups

  • HAVINGIt isUsed to filter groups formedBY
  • Example:SELECT department, AVG(salary) FROM Employees GROUP BY department HAVING AVG(salary) > 50000;

11. JOIN Operations: Combining Tables

  • INNER JOINReturns records with matching values in both tables.
  • LEFT JOIN Returns all records from the left table and matching records from the right.
  • Example:SELECT Employees.name, Departments.department_name FROM Employees INNER JOIN Departments ON Employees.department_id = Departments.department_id;

12. INNER JOIN: Retrieving Data from Multiple Tables

  • Returns only the matching rows between two tables.
  • Example:SELECT Orders.id, Customers.name FROM Orders INNER JOIN Customers ON Orders.customer_id = Customers.id;

13. LEFT JOIN: Including Non-Matching Records

  • Returns all records from the left table and matched records from the right.
  • Example:SELECT Students.name, Courses.name FROM Students LEFT JOIN Courses ON Students.id = Courses.student_id;

14. RIGHT JOIN: Complementing LEFT JOIN

  • Returns all records from the right table and matched records from the left.
  • Example:SELECT Employees.name, Projects.project_name FROM Employees RIGHT JOIN Projects ON Employees.id = Projects.employee_id;

15. FULL JOIN: Returning All Records

  • Combines and.LEFT JOINRIGHT JOIN Returns all rows from both tables.
  • Example:SELECT A.name, B.address FROM A FULL JOIN B ON A.id = B.id;

16. Self JOIN: Joining a Table with Itself

  • A self-joinSELF JOINIt iswhen a table is joined with itself.
  • Example:SELECT A.name, B.manager_name FROM Employees A, Employees B WHERE A.manager_id = B.id;

17. Subqueries: Querying Inside a Query

  • A subquery is a query nested inside another query.
  • Example:SELECT name FROM Employees WHERE department_id = (SELECT id FROM Departments WHERE name = 'HR');

18. INSERT INTO: Adding Data to Tables

  • Used to insert new rows into a table.
  • Syntax:INSERT INTO table_name (column1, column2) VALUES (value1, value2);

19. UPDATE: Modifying Data

  • Used to modify existing records in a table.
  • Syntax:UPDATE table_name SET column1 = value1 WHERE condition;
  • Example:UPDATE Employees SET salary = 50000 WHERE department = 'HR';

20. DELETE: Removing Data

  • Used to delete rows from a table.
  • Syntax:DELETE FROM table_name WHERE condition;
  • Example:DELETE FROM Employees WHERE id = 5;

21. Constraints: Ensuring Data Integrity

  • Constraints like PRIMARY KEY, FOREIGN KEY, NOT NULL, and UNIQUE ensure data validity.
  • Example:CREATE TABLE Employees (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL);

22. Primary Key: Unique Identifier for Rows

  • A unique idenAn identifierPRIMARY KEY Identifies each row in a table.
  • Example:CREATE TABLE Students (student_id INT PRIMARY KEY, name VARCHAR(100));

23. Foreign Key: Relating Tables

  • A⁣FOREIGN KEY creates a relationship between two tables. tablExample:
  • ple: CREATE TABLE Orders (order_id INT, customer_id INT, FOREIGN KEY (customer_id) REFERENCES Customers(customer_id));

24. Not Null Constraint: Preventing Null Values

  • Ensures a column cannot have a NULL value.
  • Example:CREATE TABLE Products (product_id INT NOT NULL, name VARCHAR(100) NOT NULL);

25. UNIQUE Constraint: Ensuring Uniqueness

  • Ensures that all values in a column are unique.
  • Example:CREATE TABLE Employees (email VARCHAR(100) UNIQUE);

26. ALTER TABLE: Modifying a Table Structure

  • ALTER TABLEIt isUsed to modify an existing table.
  • Example:ALTER TABLE Employees ADD COLUMN salary INT;

27. DROP TABLE: Deleting a Table

  • Removes a table and all of its data.
  • Example:DROP TABLE Students;

28. UNION: Combining Results from Multiple Queries

  • Combines results from multiple SELECTstatements.
  • Example:SELECT name FROM Employees UNION SELECT name FROM Customers;

29. DISTINCT Keyword: Removing Duplicate Data

  • Used to return only distinct (unique) values.
  • Example:SELECT DISTINCT department FROM Employees;

30. LIMIT: Restricting Query Results

  • Limits the number of rows returned by a query.
  • Example:SELECT * FROM Employees LIMIT 5;

31. BETWEEN: Filtering Within a Range

  • Filters data within a certain range.
  • Example:SELECT * FROM Employees WHERE salary BETWEEN 30000 AND 60000;

32. LIKE: Pattern Matching

  • Used to search for a specified pattern.
  • Example:SELECT * FROM Employees WHERE name LIKE 'J%';

33. IN: Matching Multiple Values

  • Filters data by matching it against a list of values.
  • Example:SELECT * FROM Employees WHERE department IN ('HR', 'Sales');

34. IS NULL: Finding Null Values

  • Filters rows with NULL values.
  • Example:SELECT * FROM Employees WHERE manager_id IS NULL;

35. CASE Statement: Conditional Logic

  • Allows for conditional logic within a query.
  • Example:SELECT name, CASE WHEN salary > 50000 THEN 'High' ELSE 'Low' END AS salary_level FROM Employees;

36. Transactions: Ensuring Data Consistency

  • A transaction ensures that a series of operations are executed successfully or rolled back.
  • Example:BEGIN TRANSACTION; UPDATE Employees SET salary = 50000; COMMIT;

37. COMMIT and ROLLBACK

  • COMMITSaves changes; ROLLBACKundoes changes made during a transaction.

38. Views: Virtual Tables

  • A viewVIEWis a virtual table that stores a query result.
  • Example:CREATE VIEW EmployeeDetails AS SELECT name, department FROM Employees WHERE salary > 50000;

39. Indexes: Speeding Up Queries

  • An index improves query performance by allowing faster searching and sorting.
  • Example:CREATE INDEX idx_department ON Employees(department);

40. Normalization: Organizing Data Efficiently

  • Process of organizing data to reduce redundancy and dependency.
  • Levels: 1NF, 2NF, 3NF, etc.

41. Denormalization: Optimizing Query Performance

  • Denormalization involves combining tables for faster querying at the expense of data redundancy.

42. Temporary Tables: Storing Data Temporarily

  • Temporary tables store data only for the duration of the session.
  • Example:CREATE TEMPORARY TABLE temp_table AS SELECT * FROM Employees;

43. Common Table Expressions (CTEs): Simplifying Queries

  • CTEs improve readability and allow for easier reuse of subqueries.
  • Example:WITH DepartmentCTE AS (SELECT department, COUNT(*) FROM Employees GROUP BY department) SELECT * FROM DepartmentCTE;

44. Recursive Queries: Handling Hierarchical Data

  • Recursive queries are used to query hierarchical data like organizational charts.
  • Example:WITH RECURSIVE OrgChart AS (SELECT id, manager_id, name FROM Employees WHERE manager_id IS NULL UNION ALL SELECT e.id, e.manager_id, e.name FROM Employees e JOIN OrgChart o ON e.manager_id = o.id) SELECT * FROM OrgChart;

45. EXPLAIN: Query Optimization

  • EXPLAINProvides information about how SQL statements are executed.
  • Example:EXPLAIN SELECT * FROM Employees WHERE salary > 50000;

46. SQL Functions: Built-in Operations

  • SQL includes various built-in functions like NOW(), LENGTH(), CONCAT(), etc.

47. SQL Injection: Security Risks

  • SQL injection occurs when malicious code is inserted into SQL queries.
  • Prevent it by using parameterized queries or stored procedures.

48. Database Backups: Data Protection

  • Regular backups ensure data recovery in case of failure.

49. Roles and Permissions: Controlling Access

  • Use roles and permissions to control user access to different parts of a database.

50. Monitoring and Logging: Tracking Changes

  • SQL logs can help monitor database activity and troubleshoot issues.

Subscribe to our website for more wonderful content.

Leave a Comment