Introduction to
SQL And PostgreSQL
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
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.
Relational Database Management System
• MySQL, Oracle, PostgreSQL
are all RDBMS
• RDBMS allows us to interact
with the database.
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.
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.
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
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
SQL Commands
SQL Commands
• Create Table:-
• Select Query:-
1.SELECT column1, column2, ...
FROM table_name;
2.SELECT * FROM table_name;
• 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;
There are also many Syntax :-
• Order by
• Insert into
• Null Values
• Delete
• IN
• Not In
• Between
• Limit
• Fetch
• Alias
There are some aggregation functions:-
• Count
• Sum
• Min
• Max
• Avg
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
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;
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;
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;
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;
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 ... );
Some other Functions
• Left and Right Sting Function
• Reverse
• Substring Function
• Replace

Sql

  • 1.
  • 2.
    Database and typesof 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 relationaldatabase 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 ManagementSystem • MySQL, Oracle, PostgreSQL are all RDBMS • RDBMS allows us to interact with the database.
  • 5.
    SQL • Structured QueryLanguage. • 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. • Tablescontain 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 DataTypes • 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, Foreignkey, 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
  • 9.
  • 10.
    SQL Commands • CreateTable:- • 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 alsomany Syntax :- • Order by • Insert into • Null Values • Delete • IN • Not In • Between • Limit • Fetch • Alias
  • 13.
    There are someaggregation functions:- • Count • Sum • Min • Max • Avg
  • 14.
    JOIN • A JOINclause 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 • TheINNER 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 • TheLEFT 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 • TheRIGHT 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 • TheFULL 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 • ASubquery 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