3rd Year Elite SQL 1&2
3rd Year Elite SQL 1&2
–1&2
Mr. Sai Sushanth Durvasula
Motivation - Data Intensive Applications
What is a DBMS?
column
Motivation for Database
Customer Care Manager wants
to know
Transformation(s)
Input Output
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
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;
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
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.
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.