SQL Workshop: Basics and Queries
SQL Workshop: Basics and Queries
Query
Language
(SQL)
Workshop-1
Introduce Yourself
Today’s What is SQL?
Numeric
• Int
• Decimal(p,d) eg Decimal(4,2)
Date and Time
• Date
• Datetime
• Time
• Timestamp
Tech.', '2022-09-21’);ID
101
me
Alex [Link] Data Analytics & Tech.
e
2022-09-01
102 Bobby [Link] Data Analytics & Tech 2022-09-21
2011 Alina [Link]. Software Engineering 2022-09-15
3016 Bren [Link]. Cloud & Network 2022-09-01
Security
100 Graham [Link] Data Analytics & Tech. 2022-09-01
81 Charlie [Link] Data Analytics & Tech. 2022-09-11
2010 Carol [Link]. Software Engineering 2022-09-15
Select Select command is used to retrieve
data from the table
statement select * from table_name;
select * from Student;
Student_ Student_Na Course Enrollment_dat
ID me e
101 Alex [Link] Data Analytics & Tech. 2022-09-01
102 Bobby [Link] Data Analytics & Tech 2022-09-21
2011 Alina [Link]. Software Engineering 2022-09-15
3016 Bren [Link]. Cloud & Network 2022-09-01
Security
100 Graham [Link] Data Analytics & Tech. 2022-09-01
81 Charlie [Link] Data Analytics & Tech. 2022-09-11
2010 Carol [Link]. Software Engineering 2022-09-15
Student_Na Enrollment_
me date
Alex 2022-09-01
Select Bobby
Alina
2022-09-21
2022-09-15
Statement Bren
Graham
2022-09-01
2022-09-01
Charlie 2022-09-11
Carol 2022-09-15
Chris 2022-09-16
select * from Student where Course = '[Link] Data Analytics & Tech.' AND
(Student_ID=101 OR Enrollment_date='2022-09-01');
Operator Description
s
= Equal to
WHERE Clause > Greater than
>= Greater than equal to
select * from Student where Enrollment_date
between '2022-09-16' and '2022-09-21'; < Less than
<= Less than equal to
!= Not Equal to
<>
select * from Student where Student_name
BETWEEN Between a certain
like 'A%'; range
LIKE Search for a pattern
select * from Student where Course IN ('[Link]
Data Analytics & Tech.', '[Link] Software IN To specify multiple
values in a column
Engineering’);
([Link])
LIKE Operator Description
Like
LIKE 'a%' with "a"
WHERE CustomerName Finds any values that end
LIKE '%a' with "a"
[Link]
IN Operator
• Used with multiple OR conditions in where clause
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
-delete the record from the table
DELETE DELETE FROM table_name WHERE condition;
Question A101
A102
USA
France
31st August
2022
31st August
98
87
2022
A103 India 30th August 90
2022
He then asks to retrieve the following information:
(a) the students
A104who arrivedPakistan
on 30th August
30th2022.
August 89
HINT (b) the student who arrived in August. 2022
(c ) countries from which the
A105 student belong
India to
5th September 78
(d) For the student with ID A105, change2022the country to Srilanka
select * from Student (e) Arrange the table according to Arrival date
where (f) Get the details of top three students
day(Enrollment_date)=
01;
SELECT TOP
-returns the number of specified records
- SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
- SELECT * FROM Student
LIMIT 3;
- SELECT * FROM Student
WHERE Enrollment_date=‘2022-09-01'
LIMIT 5;
MIN() and -returns minimum and maximum
value from the column
MAX()
SELECT MIN(column_name)
FROM table_name
WHERE condition;
SELECT MAX(column_name)
FROM table_name
WHERE condition;
COUNT(), - Count returns the number of
rows
AVG(),
SUM() - SELECT COUNT(column_name
)
FROM table_name
WHERE condition;
- SELECT AVG(column_name)
FROM table_name
WHERE condition;
- SELECT SUM(column_name)
SQL Aliases
-give alternative name to a table or a column in the table
- Use of AS keyword
- SELECT column_name AS alias_name
FROM table_name;
- SELECT column_name(s)
FROM table_name AS alias_name;
- SELECT Student_ID AS id, Student_name AS name
FROM Student;
- SELECT name, CONCAT(Address,', ',PostalCode,',
',City,', ,’Country’) AS Address FROM Employees;
Workshop-2
ALTER table -used to add, delete, or modify columns in an
existing table
-used to add and drop various constraints on an
ADD existing table.
<syntax>
-ALTER TABLE table_name
ADD column_name datatype;
<Example>
- alter table Student add Grade int;
- explain Student;
- Select * from Student;
To delete a column in a table, use the
ALTER table following syntax
COLUMN
ALTER TABLE Student
MODIFY COLUMN Student_Id
VARCHAR(12);
EXPLAIN Student;
DROP database
DROP table
TRUNCATE table
Group By •
rows
Used with aggregate functions (count(), max(), avg(),
min(), sum()) to group the result by one or more columns
<Syntax>
- SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
<Example>
- SELECT Enrollment_date, COUNT(Student_ID) FROM
Student GROUP BY Enrollment_date;
• used with GROUP BY
Having • Similar to where clause
Clause
<Syntax>
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SELECT Enrollment_date,
COUNT(Student_ID) FROM Student
GROUP BY Enrollment_date HAVING
Enrollment_date > '2022-09-15';
IN Operator
SUBQUERIES:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
Student
Student_I Student_na Cours Enrollment_da Age Weig
D me e te ht
… … … … … …
INNER JOIN
<Example>
select Student.Student_ID, Student_name,
Age, Weight from Student inner join sports
ON Student.Student_ID=sports.Student_ID;
select * from Student inner join sports ON
Student.Student_ID=sports.Student_ID;
[Link]
Left Join
[Link]
FULL OUTER JOIN
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_n
ame
WHERE condition;
[Link]
regular join, but the table is joined with itself.
Self Join SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
[Link]
[Link]
[Link]
Deleting v=w9dwX7xsBgY
Duplicate
Rows
Constraints -to uniquely identify a row
Primary Key
• E.g. NIN, SSN, student number
• not null
• unique
• no duplicates
• Primary key cannot be changed
Student_ID Primary key (Immutable)
Name NOT NULL ALTER TABLE Student MODIFY COLUMN Student_Id
varchar(12) PRIMARY KEY;
Course NOT NULL
Enrollment_d NOT NULL ALTER TABLE Student
ate ADD PRIMARY KEY (Student_ID);