This document provides an introduction to SQL and PostgreSQL. It defines what a database and relational database are, and notes that PostgreSQL is an example of a relational database management system (RDBMS). It describes how data is organized in tables with rows and columns in a relational database. The document outlines common SQL commands like CREATE TABLE, SELECT, WHERE, JOIN, and functions. It provides examples of joining multiple tables and using subqueries.
2. Database and types of Database
• Database:-A database is an organized
collection of structured information, or
data, typically stored electronically in a
computer system
Types of databases:-
• Flat File ( like excel sheet)
• Personal database.
• Document model Databases (NoSQL)
• Relational database
3. Relational
Database
• A relational database is a collection of data items with
pre-defined relationships between them. These items are
organized as a set of tables with columns and rows.
• Data is stored in Tables (relations).
• These tables can be linked together.
4. Relational Database Management System
• MySQL, Oracle, PostgreSQL
are all RDBMS
• RDBMS allows us to interact
with the database.
5. SQL
• Structured Query Language.
• It is the language for talking to relational database.
• Used to create tables, insert data ,retrieve data and much more.
• SQL Queries are very similar across different database systems.
• In SQL we learn how to create a table, add entries in a table, delete
entries from a table, modify entries in a table. We will learn about
primary key , foreign key in a table.
6. Database Tables.
• Tables contain columns
which are also called
Fields and rows of data
which are called records.
• Each columns has a
defined data type which
defines what type of data
can be contained within
the column.
• Each row of data should
be unique.
• Each column should
contain only one value per
row.
• In a relational database,
tables can be linked
together.
• Two tables are linked by
primary key and foreign
key.
7. Data Types
Numeric Data Types
• Int – For whole numbers
• Numeric (P,S) – Decimal Number
• Serial – Auto Incrementing Whole Number
String Data Types
• Char(N)-Fixed Length
• Varchar (N)
• Text
8. Primary Key, Foreign key, Unique, Not Null and Check Constraints.
• A primary key is a column or a set of columns in a table
whose values uniquely identify a row in the table.
• A foreign key is a column or a set of columns in a table
whose values correspond to the values of the primary
key in another table.
• A unique key is a group of one or more than one fields or
columns of a table which uniquely
identify database record. A unique key is the same as a
primary key, but it can accept one null value for a table
column.
• The NOT NULL constraint enforces a column
to not accept NULL values, which means that you cannot
insert or update a record without adding a value to this
field.
• The CHECK Constraint enables a condition to check the
value being entered into a record. If the condition
evaluates to false, the record violates the constraint and
isn't entered the table
10. SQL Commands
• Create Table:-
• Select Query:-
1.SELECT column1, column2, ...
FROM table_name;
2.SELECT * FROM table_name;
11. • Where:-
SELECT column1, column2, ...
FROM table_name
WHERE condition;
• Select Distinct:-
SELECT DISTINCT column1, column2, ...
FROM table_name;
• AND , OR , Not:-
1. SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
2. SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
3. SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
12. There are also many Syntax :-
• Order by
• Insert into
• Null Values
• Delete
• IN
• Not In
• Between
• Limit
• Fetch
• Alias
13. There are some aggregation functions:-
• Count
• Sum
• Min
• Max
• Avg
14. JOIN
• A JOIN clause is used to combine rows from two or
more tables, based on a related column between them.
• In SQL there are 4 types of joins.
• INNER JOIN
• OUTER JOIN
• RIGHT JOIN
• LEFT JOIN
15. INNER JOIN
• The INNER JOIN keyword selects records that have
matching values in both tables.
Syntax:-
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
16. LEFT JOINS
• The LEFT JOIN keyword returns all records from the left
table (table1), and the matched records from the right
table (table2). The result is NULL from the right side, if
there is no match.
Syntax:-
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
17. RIGHT JOIN
• The RIGHT JOIN keyword returns all records from the
right table (table2), and the matched records from the
left table (table1). The result is NULL from the left side,
when there is no match.
Syntax:-
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
18. FULL JOIN
• The FULL OUTER JOIN keyword returns all records when
there is a match in left (table1) or right (table2) table
records.
Syntax:-
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
19. Sub Query
• A Subquery or Inner query or a Nested query is a query within
another SQL query and embedded within the WHERE clause.
• A subquery is used to return data that will be used in the main query
as a condition to further restrict the data to be retrieved.
• Subqueries can be used with the SELECT, INSERT, UPDATE, and
DELETE statements along with the operators like =, <, >, >=, <=, IN,
BETWEEN, etc.
Syntax:-
SELECT column_name FROM table_name WHERE column_name expression
operator ( SELECT COLUMN_NAME from TABLE_NAME WHERE ... );
20. Some other Functions
• Left and Right Sting Function
• Reverse
• Substring Function
• Replace