0% found this document useful (0 votes)
13 views31 pages

SQL Edit

This document provides an overview of SQL and databases. It discusses what SQL is, the different types of SQL commands, and what a database is. It also describes the differences between relational and non-relational databases. Additionally, it covers data types, primary keys, foreign keys, and constraints. The document explains how to create tables in SQL, insert, update, delete, and alter values in tables. It provides examples of the SELECT statement and using the WHERE clause to filter records.

Uploaded by

Yo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views31 pages

SQL Edit

This document provides an overview of SQL and databases. It discusses what SQL is, the different types of SQL commands, and what a database is. It also describes the differences between relational and non-relational databases. Additionally, it covers data types, primary keys, foreign keys, and constraints. The document explains how to create tables in SQL, insert, update, delete, and alter values in tables. It provides examples of the SELECT statement and using the WHERE clause to filter records.

Uploaded by

Yo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

1.

Introduction to SQL-What Is SQL & Database


2. Data Types, Primary-Foreign Keys & Constraints
a. Install postgresql and pgadmin4
3. Create Table In SQL & Create Database
4. INSERT UPDATE, DELETE & ALTER Table
5. SELECT Statement & WHERE Clause with Example
6. How To Import Excel File (CSV) to SQL
7. Functions in SQL & String Function
8. Aggregate Functions – Types & Syntax
9. Group By and Having Clause
10. Time Stamp and Extract Function, Date Time Function
11. SQL JOINS – Types & Syntax
12. SELF JOIN, UNION & UNION ALL
13. Subquery
14. Window Function – Types & Syntax
15. Case Statement/Expression with examples
16. CTE- Common Table Expression with examples
WHAT IS SQL & DATABASE-
INTRODUCTION
• What is SQL
• It’s applications
• SQL v/s NoSQL
• Types of SQL Commands
• What is Database
• Excel v/s Database in SQL
SQL (Structured Query Language) is a
programming language used to interact
with database
Relational Database Non-Relational Database

SQL database NoSQL database

Data stored are either key-value pairs,


Data stored in tables document-based, graph databases or wide-
column stores
These databases have fixed or static or
They have dynamic schema
predefined schema

Low performance with huge volumes of data Easily work with huge volumes of data

Eg: PostgreSQL, MySQL, MS SQL Server Eg: MongoDB, Cassandra, Hbase


There are mainly 3 types of SQL commands:
• DDL (Data Definition Language): create, alter,
and drop
• DML (Data Manipulation Language): select,
insert, update and delete
• DCL (Data Control Language): grant and revoke
permission to users
Database is a system that allow users to
store and organise data
Excel Database
Easy to use- untrained person can work Trained person can work

Data stored less data Stores large amount of data

Good for one time analysis, quick charts Can automate tasks

No data integrity due to manual operation High data integrity

Low search/filter capabilities High search/filter capabilities


DATA TYPES, PRIMARY &
FOREIGN KEYS, CONSTRAINTS
Database
Columns Example
Tables
Data
Rows
(Rows & Columns)

RDBMS
Example











• Data type of a column defines what value the
column can store in table
• Defined while creating tables in database
• Data types mainly classified into three categories +
most used
oString: char, varchar, etc
oNumeric: int, float, bool, etc
oDate and time: date, datetime, etc
• int: used for the integer value
• float: used to specify a decimal point number
• bool: used to specify Boolean values true and false
• char: fixed length string that can contain numbers, letters, and special
characters
• varchar: variable length string that can contain numbers, letters, and
special characters
• date: date format YYYY-MM-DD
• datetime: date & time combination, format is YYYY-MM-DD hh:mm:ss
• A Primary key is a unique column we set in a table to easily identify
and locate data in queries
• A table can have only one primary key, which should be unique and
NOT NULL

• A Foreign key is a column used to link two or more tables together


• A table can have any number of foreign keys, can contain duplicate
and NULL values
Creating Database & Tables
The CREATE TABLE statement is used to create a new table in a database
• Syntax
CREATE TABLE table_name
(
column_name1 datatype constraint,
column_name2 datatype constraint,
column_name3 datatype constraint,
);

• Example
CREATE TABLE customer
(
CustID int8 PRIMARY KEY,
CustName varchar(50) NOT NULL,
Age int NOT NULL,
City char(50),
Salary numeric
);
Insert, Update, Delete
Values in Table
The INSERT INTO statement is used to insert new records in a table
• Syntax
INSERT INTO TABLE_NAME
(column1, column2, column3,...columnN)
VALUES
(value1, value2, value3,...valueN);
• Example
INSERT INTO customer
(CustID, CustName, Age, City, Salary)
VALUES
(1, ‘Sam’, 26, ‘Delhi’, 9000),
(2, ‘Ram’, 19, ‘Bangalore’, 11000),
(3, ‘Pam’, 31, ‘Mumbai’, 6000),
(4, ‘Jam’, 42, ‘Pune’, 10000);
The UPDATE command is used to update existing rows in a table
• Syntax
UPDATE TABLE_NAME
SET “Column_name1” = ‘value1’, “Column_name2” = ‘value2’
WHERE “ID” = ‘value’

• Example
UPDATE customer
SET CustName = 'Xam’, Age= 32
WHERE CustID = 4;
The ALTER TABLE statement is used to add, delete, or modify columns
in an existing table
• ALTER TABLE - ADD Column Syntax
ALTER TABLE table_name
ADD COLUMN column_name ;
• ALTER TABLE - DROP COLUMN Syntax
ALTER TABLE table_name
DROP COLUMN column_name;
• ALTER TABLE - ALTER/MODIFY COLUMN Syntax
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
The DELETE statement is used to delete existing records in a table
• Syntax
DELETE FROM table_name WHERE condition;

• Example
DELETE FROM customer
WHERE CustID = 3;
The DROP TABLE command deletes a table in the database
• Syntax
DROP TABLE table_name;

The TRUNCATE TABLE command deletes the data inside a table, but
not the table itself
• Syntax
TRUNCATE TABLE table_name;
SELECT & WHERE CLAUSE
CREATE TABLE classroom
(
rollno int8 PRIMARY KEY,
name varchar(50) NOT NULL,
house char(12) NOT NULL,
grade char(1)
);

INSERT INTO classroom


(rollno, name, house, grade)
VALUES
(1, ‘Sam’, ‘Akash’, ‘B’),
(2, ‘Ram’, ‘Agni’, ‘A’),
(3, ‘Shyam’, ‘Jal’, ’B’),
(4, ‘Sundar’, ‘Agni’, ’A’),
(5, ‘Ram’, ‘Yayu’, ‘B’);
The SELECT statement is used to select data from a database.
• Syntax
SELECT column_name FROM table_name;

To select all the fields available in the table


• Syntax
SELECT * FROM table_name;

To select distinct/unique fields available in the table


• Syntax
SELECT DISTINCT Column_name FROM table_name;
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition
• Syntax
SELECT column_name FROM table_name
WHERE conditions;

• Example
SELECT name FROM classroom
WHERE grade=‘A’;
The SQL reserved words and characters are called operators, which are
used with a WHERE clause in a SQL query
Most used operators:
1. Arithmetic operators : arithmetic operations on numeric values
Example: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%)
2. Comparison operators: compare two different data of SQL table
• Example: Equal (=), Not Equal (!=), Greater Than (>), Greater Than Equals to (>=)
3. Logical operators: perform the Boolean operations
• Example: ALL, IN, BETWEEN, LIKE, AND, OR, NOT, ANY
4. Bitwise operators: perform the bit operations on the Integer values
• Example: Bitwise AND (&), Bitwise OR(|)
END

You might also like