SQL Notes (1)
SQL Notes (1)
Table of Contents
2. Data Types, Primary -Foreign Keys & Constraints a. Install postgresql and pgadmin4
13. Subquery
Database:
Example:
Output:
The query will return all rows and columns from the employees table.
Syntax:
Example:
2. What Is SQL & Database Syntax with Examples and Output of Code
Creating a Database:
Syntax:
Example:
Output:
Introduction: SQL supports various data types for defining the nature of data to be stored in a
table's columns.
Syntax:
Example:
4. Primary & Foreign Keys Syntax with Examples and Output of Code
Introduction: Primary keys uniquely identify each record in a table, while foreign keys establish
relationships between tables.
Syntax:
Introduction: Constraints enforce rules at the column level, ensuring data integrity.
Syntax:
Output:
Steps:
1. Install PostgreSQL:
○ On Ubuntu: sudo apt-get install postgresql postgresql-contrib
○ On Windows: Download the installer from the PostgreSQL website.
2. Install pgAdmin4:
○ On Ubuntu: sudo apt-get install pgadmin4
○ On Windows: pgAdmin is included in the PostgreSQL installer.
Syntax:
Example:
Output:
Syntax:
Example:
Output:
Example:
Output:
Syntax:
Example:
Output:
● The department ID for the employee with employee_id 1 is updated to 2.
Syntax:
Example:
Output:
Syntax:
Example:
Output:
13. SELECT Statement & Syntax with Examples and Output of Code
Syntax:
Example:
Syntax:
Example:
15. How To Import Excel File (CSV) to SQL Syntax with Examples and Output of Code
Syntax:
Example:
Output:
● Data from the CSV file is imported into the employees table.
Syntax:
Example:
Example:
18. Aggregate Functions – Types & Syntax with Examples and Output of Code
Syntax:
Example:
19. Group By Syntax with Examples and Output of Code
Syntax:
Example:
20. Having Clause Syntax with Examples and Output of Code
Syntax:
Example:
Introduction: A timestamp is a data type that stores both date and time information.
Syntax:
Example:
Output:
● A record is inserted with the current date and time in the event_time column.
22. Extract Function with Syntax with Examples and Output of Code
Introduction: The EXTRACT function is used to retrieve a part of a date or time value.
Syntax:
Example:
23. Date Time Function Syntax with Examples and Output of Code
Introduction: Date and time functions in SQL are used to manipulate date and time values.
Common Functions:
Example:
24. SQL JOINS – Types & Syntax with Examples and Output of Code
Introduction: Joins are used to combine rows from two or more tables based on a related column.
Types:
Syntax:
Example:
Introduction: A self-join is a regular join but the table is joined with itself.
Syntax:
Example:
Introduction: The UNION operator is used to combine the result-set of two or more SELECT
statements.
Syntax:
Example:
Output:
Introduction: The UNION ALL operator is similar to UNION, but it includes all duplicates.
Syntax:
Example:
Output:
Syntax:
Example:
Output:
29. Window Function – Types Syntax with Examples and Output of Code
Introduction: Window functions perform a calculation across a set of table rows related to the
current row.
Syntax:
Example:
30. Case Statement/Expression Syntax with Examples and Output of Code
Introduction: The CASE statement is used to return values based on different conditions.
Syntax:
Example:
31. CTE - Common Table Expression with Examples and Output of Code
Introduction: A CTE (Common Table Expression) is a temporary result set that can be referenced
within a SELECT, INSERT, UPDATE, or DELETE statement.
Syntax:
Example:
These detailed notes will serve as a comprehensive guide for learning SQL, covering syntax,
examples, and expected outputs for each SQL operation.
Revise SQL Course
BASIC to ADVANCE
STRING FUNCTION
AGGREGATE
FUNCTION