0% found this document useful (0 votes)
14 views

3rd Year Elite SQL 1&2

Uploaded by

Arnav Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

3rd Year Elite SQL 1&2

Uploaded by

Arnav Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

3 rd year elite SQL

–1&2
Mr. Sai Sushanth Durvasula
Motivation - Data Intensive Applications

• What information do the


customer care executive collect
from us?
Attribute Value
Name Bob
Phone Number 987654321
Email [email protected]
Compliant/Issue Recharge Amount is not
reflecting
Creation Date 8th Sept, 2023
Status In Progress
Motivation for Database

How do we store this info for


more than one customer?
File System – csv , tsv or text?
Compliant Creation Status
Name Phone Number Email /Issue Date

Bob 987654321 [email protected] Recharge 7th Sept, In


om Amount is 2023 Progress
not
reflecting

Rosy 987654123 [email protected] Net not 8th


m working Sept,2023
Definitions
What is a Database?

Database is collection of data in a format that can be


easily fetched and managed!

What is a DBMS?

A software application used to manage our DB is called


DBMS (Database Management System)
Motivation for Database
Data => store in Tables in a
Database!

How do we talk to Database?

SQL – Structured Query Language


Motivation – How do we store this info
for more than one customer?
• Database => Tables!

Compliant Creation Status


Name Phone Number Email /Issue Date

Bob 987654321 [email protected] Recharge 7th Sept, In


om Amount is 2023 Progress row
not
reflecting

Rosy 987654123 [email protected] Net not 8th


m working Sept,2023

column
Motivation for Database
Customer Care Manager wants
to know

1) How many issues have been


resolved today?
2) What all issues are in progress
for more than a day?
What is SQL?
Structured Query Language
SQL is a programming language used to interact with
relational databases.

It is used to perform CRUD operations :


Create
Read
Update
Delete
SQL Query as a way to solve a problem!

Transformation(s)

Input Output

transformations are functions in DSA


world
and transformations are sql queries
in DB world!
Example

Input Transformation1 Transformation2 Output


Relational and NOSQL Database
RDBMS NOSQL
What do we
mean by
Relational?
ACID properties
Motivation – IMDB Data Model
Row vs Columnar Stores
Row vs Columnar Stores
Row vs Columnar Stores
Creating our First Database
Our first SQL Query

CREATE DATABASE db_name;


DROP DATABASE db_name;

Ex: create database elite_db;


Types of SQL Commands
DDL (Data Definition Language) :
create, alter, rename, truncate & drop
DML (Data Manipulation Language) :
insert, update & delete
DCL (Data Control Language) :
grant & revoke permission to users
DQL (Data Query Language) :
select
TCL (Transaction Control Language) :
start transaction, commit, rollback etc
MySQL

MySQL is an open-source relational database management system.

Its name is a combination of "My", the name of co-founder Michael Widenius's daughter My, and
"SQL", the acronym for Structured Query Language

What is a Database Table?


A table is a collection of related data entries, and it consists of columns and rows.
A column holds specific information about every record in the table.
A record (or row) is each individual entry that exists in a table.
Table related Queries
Create
CREATE TABLE table_name (
column_name1 datatype constraint,
column_name2 datatype constraint,
);

Ex: create table tbl1 (


name varchar(30),
phone_num varchar(15),
email varchar(20),
created_at date
);
Table related Queries - SELECT
It is used to select data from a database.
The data returned is stored in a result table, called the
result-set.

Ex: 1
SELECT *
FROM table_name; -- all columns

Ex: select *
from tbl1;
Table related Queries - SELECT
Ex: 2
SELECT col1
, col3
FROM table_name; -- only specified columns

SELECT CustomerName
, City
, Country
FROM Customers;

CustomerName City Country


Alakananda Delhi India
Arjun kanpur India
Aravind pinniton UK
Filtering using WHERE
WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.

SELECT column1, column2, ...


FROM table_name
WHERE condition;

WHERE clause is not only used in SELECT but also in UPDATE, DELETE
WHERE example
SELECT *
FROM Customers
WHERE Country = ‘India’;
AND, OR, NOT
WHERE clause can be combined with AND, OR, NOT operators

These operators are used to filter records based on more than one
condition:
• The AND operator displays a record if all the conditions
separated by AND are TRUE
• The OR operator displays a record if all the conditions separated
by OR are TRUE
• The NOT operator displays a record if all the condition Is NOT
TRUE
AND
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

OR
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

NOT
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
BETWEEN

This operator selects values within a given range.


The values can be numbers, text, or dates.

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Aliases
Aliases are used to give a table, or a column in a
table, a temporary name.

An alias is created with the AS keyword

SELECT column_name AS alias_name


FROM table_name;
Aliases
Ex:
SELECT CustomerID AS ID
, CustomerName AS “Customer Name”
FROM Customers;

ID Customer Name
2 Alakananda
3 Arjun
4 Aravind
Table related Queries - Insert
INSERT INTO table_name
(colname1, colname2);
VALUES
(col1_v1, col2_v1),
(col1_v2, col2_v2);

Ex1:
insert into tbl1 (name,phone_num)
values ('Bob',987654321);
insert into tbl1 (name)
values ('Rose');insert into tbl1
(name,phone_num)values ('Bob',987654321),
('Rosy',87856464);
Table related Queries - Insert
INSERT INTO table_name
(colname1, colname2);
VALUES
(col1_v1, col2_v1),
(col1_v2, col2_v2);

Ex1: Ex3:
insert into tbl1 (name,phone_num) insert into tbl1 (name,phone_num)
values ('Bob',987654321); values ('Bob',987654321),
('Rosy',87856464);
Ex2:
insert into tbl1 (name)
values ('Rose');
Keys
Primary Key
It is a column (or set of columns) in a table that uniquely
identifies each row. (a unique id) There is only 1 PK & it should
be NOT null.

Foreign Key
A foreign key is a column (or set of columns) in a table that refers
to the primary key in another table.
There can be multiple FKs.
FKs can have duplicate & null values.

You might also like