You are on page 1of 37

SQL Tutorial

1. Basic terminologies
2. Types of SQL
3. SQL constraints
4. Functions in SQL
5. SQL joins
Why we need data base?
1. Traditionally, data was organized in file formats but data fetching
and writing would not be so fast and easy with those type of
systems.
2. Managing large amount of data is a problem
3. Using spreadsheets you can handle but it affects efficiency
4. DB provides flexibility
5. Provides security
What is DBMS?
• Data Base Management System is a software used to store and manage
data or we can say it is a collection of programs which enables users to
access database, manipulate data, report/ represent data.
• DBMS stores data in such a way that it becomes easier to retrieve,
manipulate, and produce information.
• DBMS was first implemented in 1960s.
• Charles Bachman’s Integrated Data Store(IDS) is considered as the
first DBMS in history.
What is SQL?
• Structured Query Language – lang to communicate with db.
• RDBMS – type of db.
• In this data is stored in tables, in the form of rows and columns.
• It stores data over a number of tables instead of one table.
• It is used to modify and access data or information from storage area called
database.
• Few features are :
• Provides data to be stored in tables.
• Tables are created using SQL.
• We can retrieve data from tables using SQL.
MySQL Database
• MySQL is a fast, easy-to-use RDBMS being used for many small and
big businesses. MySQL is developed, marketed and supported by
MySQL AB, which is a Swedish company. MySQL is becoming so
popular because of many good reasons −
• MySQL is released under an open-source license. So you have nothing to pay to use it.
• MySQL works on many operating systems and with many languages including PHP,
PERL, C, C++, JAVA, etc.
• MySQL works very quickly and works well even with large data sets.
• MySQL supports large databases, up to 50 million rows or more in a table. The default
file size limit for a table is 4GB, but you can increase this (if your operating system can
handle it) to a theoretical limit of 8 million terabytes (TB).
Basic technologies :
1. Table :
Data in database is stored in the tables. Table consists of rows and
columns.
eg,.,
Person
ID Name Age Address Salary

1 Madhura 30 Pune 20000

2 Shyam 42 Mumbai 15000


2. Fields :
• Every table is broken up into smaller entities called fields.
• The fields in Person table are ID, Name, Age, Address, Salary i.e.,
a field is a column in a table.
• Each column in a table is required to have a name and data types.

ID Name Age Address Salary


1 Madhura 30 Pune 20000
2 Shyam 42 Mumbai 15000
3 Ram 25 USA 250000
3. Record or Row :
• A record, also called as a row of data, is each individual entry that
exists in a table.
• A record is a horizontal entity in a table.
e.g.,
From Person table,
1 Madhura 30 Pune 20000

2 Shyam 42 Mumbai 15000


Types of SQL Queries:
• Q. What is a Query?
-> A query is a set of instructions given to DBMS. It tells database what
information you would like to get or set from/to database.

1. DDL (Data Definition Language)


2. DML (Data Manipulation Language)
3. DCL (Data Control Language)
4. DQL (Data Query Language)
DDL DML DQL DCL

create insert select grant

alter update revoke

drop delete

truncate
SQL Constraints :
• Constraints are simply restrictions placed on one or more columns of a
table to limit the type of values that can be stored in the column.
• Few constraints are :

1. NOT NULL
2. Primary key
3. Unique Key
1. not null
• It specifies that column does not accept null value.
e.g.,
create table student(id int not null, name varchar(55));
-> table created successfully..
insert into student values(1, ‘James’);
-> Record inserted successfully..
insert into student values(null, ‘Bond’);
-> Column ‘id’ can not be null..
2. Primary key :
• It is a special column assigned to uniquely identify all table records.
• No two rows of a table can have the same primary key value.
• Also you can not insert null value in a primary key.
• In simple words it restricts duplicate values.
e.g., create table student(id int primary key, name varchar(55));
-> table created successfully..
insert into student values(1, ‘James’);
-> Record inserted successfully..
insert into student values(1, ‘James’);
-> Duplicate entry ‘1’ for key primary..
insert into student values(null, ‘Bond’);
-> Column ‘id’ can not be null..
• Only one column can be set as primary key.
e.g., create table student(id int primary key, name varchar(55) primary key);
-> Multiple primary key defined..
3. Unique key :
• It also ensures data uniqueness like primary key.
• Only differences are that,

Unique key allows null values,


Unique key can have multiple null values.
only one column can be set as primary key but multiple columns can be set as
unique key.
Unique key continued ..

• E.g.,
create table student(id int primary key, name varchar(55) unique key,
salary int unique key);
-> table created successfully..
insert into student values(1, ‘James’, 10000);
-> Record inserted successfully..
insert into student values(2, ‘Bond’, null);
-> Record inserted successfully..
SQL functions :
• Commonly used functions are as follows :
1. AVG()
2. MAX() Student table :
3. MIN() Rollno Name Marks
1 James 50
4. SUM() 2 Robert 50
5. COUNT() 3 Harry 80

Consider table on top side for example :


1. AVG()
• Returns average value of a numeric column.
• Syntax :
select avg(column name) from table name;

Select avg(Marks) from Student;


-> 60
2. MAX()
• Returns max value from particular column.
• Syntax :
select max(column name) from table name;

select max(Marks) from Student;


->80
3. MIN()
• Returns min value of a particular table.
• Syntax :
select min(column name) from table name;

select min(Marks) from Student;


-> 50
4. SUM()
• Returns sum of elements present in particular column.
• Syntax :
select sum(column name) from table name;

select min(Marks) from Student;


-> 180
5. COUNT()
• Returns count of elements present in particular column, i.e, number of
rows in that column.
• Syntax :
select count(column name) from table name;

select count(Marks) from Student;


-> 3
SQL Joins :
• It is used to combine rows from two or more tables based on a
common field/column between them.
• Types of joins are :

1. Inner join
2. Left join
3. Right join
4. Full join
Two rules for SQL joins :

1. Both the tables should have at least one common column between
them
2. Make sequence, i.e., in which order you want to join both the tables,
decide that first.
• Consider following two tables, we will perform join operations on
these two tables,
• Id from stud, name, surname, marks, age, id from stud_info
Student : id name marks
1 James 50
same column name 2 Harry 60
3 Emma 80
Different id

Student_info :
same column name id surname age
1 Bond 56
2 Potter 25
4 ABC 21
Different id
1. Inner join :
• Common elements of both tables.
2. Left join :
• All elements from left side table.
3. Right join :
• All elements from right side table.
4. Full join :
• All the elemnts in both the tables.
• Now first lets decide sequence,
id, name, surname, marks, age, id
id -> student table
name -> student table
surname -> student_info table
marks -> student table
age -> student_info table
id -> student_info table
Inner join query :
• select student.id, student.name, student_info .surname, student.marks,
student_info .age, student_info .id
from student join student_info
on student.id = student_info.id;

id name surname marks age Id


1 James Bond 50 56 1
2 Harry Potter 60 25 2
Left join query :
• select student.id, student.name, student_info.surname, student.marks, student_info.age,
student_info.id
from student left join student_info
on student.id = student_info.id;

id name surname marks age Id


1 James • Bond id 50 56 1
2 Harry • Potter name 60 25 2
• surname
3 Emma • NULL marks 80 NULL NULL
• age
• Id
Right join query :
• select student.id, student.name, student_info.surname, student.marks,
student_info.age, student_info.id
from student right join student_info
on student.id = student_info .id;
id name surname marks Age Id
1 James Bond 50 56 1
2 Harry Potter 60 25 2
NULL NULL abc NULL 21 4
Full join query :
• select student.id, student.name, student_info.surname, student.marks,
student_info.age, student_info.id
from student FULL JOIN student_info
on student.id = student_info.id;
id name surname marks age Id
1 James Bond 50 56 1
2 Harry Potter 60 25 2
3 Emma NULL 80 NULL NULL
NULL NULL Abc NULL 21 4
Sub query :

Query within query is called subquery


Sub query :

Query within query is called subquery


• select *
from employee
where id in (select id
from employee
where salary > 4500);
Assignment :

• Difference between primary key and unique key?


• Can a table have multiple unique and primary key?
• Difference between truncate, delete and drop queries?

You might also like