Professional Documents
Culture Documents
Databaseusing My SQL
Databaseusing My SQL
Databaseusing My SQL
Module Overview
SQL is the query language—it’s used for accessing, cleaning, and analyzing data that are stored in databases. It’s very
easy to learn, yet it’s employed by the world’s largest companies to solve incredibly challenging problems.
This module will aim at making you understand and implement the SQL queries.
Module Objective
At the end of the module, you will be able,
• To understand core database concepts
• To understand how data is stored in tables
• To understand DDL, DCL, DML commands
• To know the sorting of data using SQL
• To know the functions in SQL
• To understand the essential topics such as Sub-queries, Joins, views, unions, indexes, etc.
• To understand triggers and cursors
Depending upon the usage requirements, the figure depicts various types of databases available in the
market. Few of the databases are explained below
➢ Hierarchical –
• This type of DBMS employs the "parent-child" relationship of storing data. This type of DBMS is rarely
used nowadays. Its structure is like a tree with nodes representing records and branches representing
fields.In the Hierarchical Database Model we have to learn about the databases.
• It is very fast and simple.
• The structure implies that a record can have also a repeating information.
• In this structure Data follows a series of records, It is a set of field values attached to it. It collects all
records together as a record type.
➢ Network DBMS –
• This type of DBMS supports many-to many relations. This usually results in complex database structures.
• A network databases are mainly used on a large digital computers.
• It more connections can be made between different types of data, network databases are considered
more efficiency It contains limitations must be considered when we have to use this kind of database.
• It is Similar to the hierarchical databases, network databases.
• Network databases are similar to hierarchical databases by also having a hierarchical structure.
• A network database looks more like a cobweb or interconnected network of records.
• In network databases, children are called members and parents are called occupier.
• The difference between each child or member can have more than one parent.
What is NoSQL ?
➢ The main characteristic of NoSQL is its non-adherence to Relational Database Concepts. NOSQL means "Not
only SQL".
➢ Concept of NoSQL databases grew with internet giants such as Google, Facebook, Amazon etc who deal with
gigantic volumes of data.
➢ When you use relational database for massive volumes of data , the system starts getting slow in terms of
response time. To overcome this , we could of course "scale up" our systems by upgrading our existing
hardware.
➢ The alternative to the above problem would be to distribute our database load on multiple hosts as the load
increases.This is known as "scaling out".
➢ NOSQL database are non-relational databases that scale out better than relational databases and are
designed with web applications in mind.
➢ NOSQL database do not use SQL to query the data and do not follow strict schemas like relational
models.With NoSQL, ACID (Atomicity, Consistency, Isolation, Durability) features are not guaranteed always.
➢ NoSQL databases are especially useful for working with large sets of distributed data.
➢ NoSQL database examples include MongoDB, BigTable, Redis, RavenDB Cassandra, HBase, Neo4j and
CouchDB.
What is MySQL?
➢ MySQL is a freely available open source Relational Database Management System (RDBMS) that uses
Structured Query Language (SQL).
✓ high performance
✓ Cost effective
✓ Cross platform
RDBMS Concepts
➢ RDBMS stands for Relational Database Management System.
➢ RDBMS is the basis forSQL, and for all modern database systems like MS SQL Server, IBM DB2,
Oracle,
MySQL, and Microsoft Access.
➢ A Relational database management system (RDBMS) is a database management system (DBMS)
that
is based on therelational model as introduced by E. F. Codd.
What is a table?
➢ The data in an RDBMS is stored in database objects which are called as tables.
➢ This table is basically a collection of related data entries and it consists of numerous columns and
rows.
➢ "Table" is another term for "relation"
+----+----------+-----+-----------+----------+
• A record is also called as a row of data is each individual entry that exists in a table.
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
• A column is a vertical entity in a table that contains all information associated with a specific field
in a table.
For example, a column in the CUSTOMERS table is ADDRESS, which represents location description and
would be as shown below −
+-----------+
| ADDRESS |
| Ahmedabad |
| Delhi |
| Kota |
| Mumbai |
| Bhopal |
| MP |
| Indore |
+----+------+
What is Normalization?
➢ Database normalization is the process of efficiently organizing data in a database. There are two
reasons of
this normalization process −
• Eliminating redundant data, for example, storing the same data in more than one table.
• Ensuring data dependencies make sense.
➢ It divides larger tables to smaller tables and links them using relationships.
➢ Normalization rules are divided into the following normal forms:
• First Normal Form
• Second Normal Form
• Third Normal Form
• BCNF
1. First Normal Form
• Eliminate repeating groups in individual tables.
• Create a separate table for each set of related data.
1. Unnormalized table:
Adv-
Advis Class Class
Student# Roo Class3
or 1 2
m
Jone 41 101 143 159-
1022
s 2 -07 -01 02
Smit 21 201 211 214-
4123
h 6 -01 -02 01
Tables should have only two dimensions. Since one student has several classes, these classes
should be listed in a separate table. Fields Class1, Class2, and Class3 in the above records are
indications of
design trouble.
Note the multiple Class# values for each Student# value in the above table. Class# is not
functionally dependent on Student# (primary key), so this relationship is not in second
normal form.
Students:
Student# Class#
1022 101-07
1022 143-01
1022 159-02
4123 201-01
4123 211-02
4123 214-01
In the last example, Adv-Room (the advisor's office number) is functionally dependent on the
Advisor attribute. The solution is to move that attribute from the Students table to the
Faculty table,
as shown below:
Students:
Student# Advisor
1022 Jones
4123 Smith
Faculty:
Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers"
or "Orders"). Tables contain records (rows) with data.
Tips : SQL keywords are NOT case sensitive: select is the same as SELECT
Some database systems require a semicolon at the end of each SQL statement.
Semicolon is the standard way to separate each SQL statement in database systems that allow more than
one SQL statement to be executed in the same call to the server.
DATA TYPES
Data types define the nature of the data that can be stored in a particular column of a table
1. Numeric,
2. Text
3. Date/time.
Numeric data types are used to store numeric values. It is very important to make sure range of your data
is between lower and upper boundaries of numeric data types.
DECIMAL( , ) A DOUBLE stored as a string , allowing for a fixed decimal point. Choice for storing
currency values.
As data type category name implies these are used to store text values. Always make sure you length of
your textualdata do not exceed maximum lengths.
3.Date / Time
DATE YYYY-MM-DD
TIMESTAMP YYYYMMDDHHMMSS
TIME HH:MM:SS
Apart from above there are some other data types in MySQL.
SET This is also used for storing text values chosen from a list of predefined text values. It can
have multiple values.
SQL: Commands
SQL Commands
SQL commands are mainly categorized into five categories as discussed below:
• DDL
• DML
• DRL
• DCL
• TCL
i.CREATE DATABASE
A Database is defined as a structured set of data. So, in SQL the very first step to store the data in a well structured
manner is to create a database. The CREATE DATABASEstatement is used to create a new database in SQL.
Syntax:
Example:
This query will create a new database in SQL and name the database as my_database.
ii.CREATE TABLE
The CREATE TABLE statement is used to create a table in SQL. We know that a table comprises of rows and columns. So
while creating tables we have to provide all the information to SQL about the names of the columns, type of data to be
stored in columns, size of the data etc. Let us now dive into details on how to use CREATE TABLE statement to create
tables in SQL.
Syntax:
ExampleQuery:
This query will create a table named Students with three columns, ROLL_NO, NAME and SUBJECT.
If you want to delete an existing database <test>, then the DROP DATABASE statement would be as shown below −
NOTE − Be careful before using this operation because by deleting an existing database would result in loss of complete
information stored in the database.
iii.ALTER TABLE-MODIFY
It is used to modify the existing columns in a table. Multiple columns can also be modified at once.
Syntax:
Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22| MP |4500.00|
|7|Muffy|24|Indore|10000.00|
+----+----------+-----+-----------+----------+
Now, the CUSTOMERS table is changed and following would be output from the SELECT statement.
+----+---------+-----+-----------+----------+------+
| ID | NAME | AGE | ADDRESS | SALARY | SEX |
+----+---------+-----+-----------+----------+------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | NULL |
| 2 | Ramesh | 25 | Delhi | 1500.00 | NULL |
| 3 | kaushik | 23 | Kota | 2000.00 | NULL |
| 4 | kaushik | 25 | Mumbai | 6500.00 | NULL |
| 5 | Hardik | 27 | Bhopal | 8500.00 | NULL |
| 6 | Komal | 22 | MP | 4500.00 | NULL |
| 7 | Muffy | 24 | Indore | 10000.00 | NULL |
+----+---------+-----+-----------+----------+------+
Following is the example to DROP sex column from the existing table.
Now, the CUSTOMERS table is changed and following would be the output from the SELECT statement.
+----+---------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+---------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Ramesh | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | kaushik | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+---------+-----+-----------+----------+
4)TRUNCATE–is used to remove all records from a table, including all spaces allocated for the records are removed.
Examples of DML:
1)SELECT – is used to retrieve data from the a database.
Basic Syntax:
• Query to fetch the fields ROLL_NO, NAME, AGE from the table Student:
Column names and values both: In the second method we will specify both the columns which we want to fill and
their corresponding values as shown below:
Syntax:
INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2, value3,..);
value1, value2, value3 : value of first column, second column,... for the new record
DROPstatement:
The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be
removed. No DML triggers will be fired. The operation cannot be rolled back.
Clause Description
Example
OR
The above command will alter the user details and will provide it access to unlimited tablespace on system.
NOTE: Generally unlimited quota is provided to Admin users.
COMMIT;
ROLLBACK TO savepoint_name;
SAVEPOINT savepoint_name;
In short, using this command we can name the different states of our data in any table and then rollback to that state
using the ROLLBACK command whenever required.
1 Abhi
2 Adam
4 Alex
Lets use some SQL queries on the above table and see the results.
NOTE: SELECT statement is used to show the data stored in the table.
The resultant table will look like,
id name
1 Abhi
4 Alex
5 Abhijit
6 Chris
7 Bravo
Now let's use the ROLLBACK command to roll back the state of data to the savepoint B.
ROLLBACK TO B;
SELECT * FROM class;
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
Now let's again use the ROLLBACK command to roll back the state of data to the savepoint A
ROLLBACK TO A;
SELECT * FROM class;
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
So now you know how the commands COMMIT, ROLLBACK and SAVEPOINT works.
Lab Activity:
Students kindly perform the above discussed topics in your lab sessions.
Operators
SQL : OPERATORS
• These Operators are used to specify conditions in an SQL statement and to serve as conjunctions for multiple
conditions in a statement.
❖ Arithmetic operators
❖ Comparison operators
❖ Logical operators
Assume 'variable a' holds 10 and 'variable b' holds 20, then −
Show Examples
Assume 'variable a' holds 10 and 'variable b' holds 20, then −
Show Examples
Checks if the values of two operands are equal or not, if yes then (a = b) is
= condition becomes true. not
true.
Checks if the values of two operands are equal or not, if values are not (a != b)
!=
equal then condition becomes true. is true.
Checks if the values of two operands are equal or not, if values are not (a <> b)
<>
equal then condition becomes true. is true.
Checks if the value of left operand is greater than the value of right (a > b) is
> operand, if yes then condition becomes true. not
true.
Checks if the value of left operand is less than the value of right operand, (a < b) is
<
if yes then condition becomes true. true.
Checks if the value of left operand is greater than or equal to the value of (a >= b)
>= right operand, if yes then condition becomes true. is not
true.
Checks if the value of left operand is not less than the value of right (a !< b)
!<
operand, if yes then condition becomes true. is false.
Checks if the value of left operand is not greater than the value of right (a !> b)
!>
operand, if yes then condition becomes true. is true.
Show Examples
ALL
1
The ALL operator is used to compare a value to all values in another value set.
AND
2
The AND operator allows the existence of multiple conditions in an SQL statement's
WHERE clause.
ANY
3
The ANY operator is used to compare a value to any applicable value in the list as per the
condition.
4 BETWEEN
EXISTS
5
The EXISTS operator is used to search for the presence of a row in a specified table that
meets a certain criterion.
IN
6
The IN operator is used to compare a value to a list of literal values that have been
specified.
LIKE
7
The LIKE operator is used to compare a value to similar values using wildcard operators.
NOT
8
The NOT operator reverses the meaning of the logical operator with which it is used. Eg:
NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
OR
9
The OR operator is used to combine multiple conditions in an SQL statement's WHERE
clause.
IS NULL
10
The NULL operator is used to compare a value with a NULL value.
11 UNIQUE
Examples:
AND Example : The following SQL statement selects all fields from "Customers" where country is "Germany" AND city
is “Berlin”.
OR Example: The following SQL statement selects all fields from "Customers" where city is "Berlin" OR "München":
NOT Example: The following SQL statement selects all fields from "Customers" where country is NOT "Germany":
Lab Activity:
Students kindly perform the above discussed topics in your lab sessions.
Expressions
An expression is a combination of one or more values, operators and SQL functions that evaluate to a value. These SQL
EXPRESSIONs are like formulae and they are written in query language. You can also use them to query the database
for a specific set of data.
There are different types of SQL expressions, which are mentioned below −
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22| MP |4500.00|
|7|Muffy|24|Indore|10000.00|
+----+----------+-----+-----------+----------+
The following table is a simple example showing the usage of various SQL Boolean Expressions −
+----+-------+-----+---------+----------+
+----+-------+-----+---------+----------+
|7|Muffy|24|Indore|10000.00|
+----+-------+-----+---------+----------+
2. Numeric Expression
These expressions are used to perform any mathematical operation in any query. Following is the syntax −
Here, the numerical_expression is used for a mathematical expression or any formula. Following is a simple example
showing the usage of SQL Numeric Expressions −
+----------+
| ADDITION |
+----------+
|21|
+----------+
There are several built-in functions like avg(), sum(), count(), etc., to perform what is known as the aggregate data
calculations against a table or a specific table column.
+---------+
| RECORDS |
|7|
+---------+
3. Date Expressions
Date Expressions return current system date and time values −
+---------------------+
|Current_Timestamp|
+---------------------+
|2009-11-1206:40:23|
+---------------------+
+-------------------------+
| GETDATE |
+-------------------------+
|2009-10-2212:07:18.140|
+-------------------------+
Lab Activity:
Students kindly perform the above discussed topics in your lab sessions.
Here, students use CREATE and INSERT command to create the table and insert the values.
2. Use SELECT command to display the table created above.
3. Use ALTER and then DROP command to drop the column “commission”.
3. Use ALTER command and then ADD command to add column “commission” back.
4. Use UPDATE column to add some values to the column “commission” where dep_id=1001 OR dep_id=3001.
5. Use DELETE command to delete rows where job_name=”ANALYST”.
6. Use DELETE command to delete rows where job_name=”MANAGER” and emp_name=”CLARE”.
6. Use UPDATE command to add manager_id as 68310 where job_name=”PRESIDENT”.
7. Use TRUNCATE command to remove all the rows from the table. And then use SELECT command to check whether
the table contents exist or not.
8. Use DROP command to drop the table.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
The following code block has an example, which would sort the result in an ascending order by the NAME and the
SALARY −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+----------+-----+-----------+----------+
The following code block has an example, which would sort the result in the descending order by NAME.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
+----+----------+-----+-----------+----------+
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups. This
GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause.
Syntax
The basic syntax of a GROUP BY clause is shown in the following code block. The GROUP BY clause must follow the
conditions in the WHERE clause and must precede the ORDER BY clause if one is used.
Example
Consider the CUSTOMERS table is having the following records –
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
If you want to know the total amount of the salary on each customer, then the GROUP BY query would be as follows.
GROUP BY NAME;
+----------+-------------+
| NAME | SUM(SALARY) |
+----------+-------------+
| Chaitali | 6500.00 |
| Hardik | 8500.00 |
| kaushik | 2000.00 |
| Khilan | 1500.00 |
| Komal | 4500.00 |
| Muffy | 10000.00 |
| Ramesh | 2000.00 |
+----------+-------------+
Syntax
The following code block shows the position of the HAVING Clause in a query.
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
Example
Consider the CUSTOMERS table having the following records.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
Following is an example, which would display a record for a similar age count that would be more than or equal to 2.
FROM CUSTOMERS
GROUP BY age
+----+--------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+--------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
Wild Cards
The SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used
in conjunction with the LIKE operator.
Syntax
The basic syntax of % and _ is as follows −
or
or
or
or
Example
The following table has a few examples showing the WHERE part having different LIKE clause with '%' and '_' operators
−
Following is an example, which would display all the records from the CUSTOMERS table, where the SALARY starts with
200.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
+----+----------+-----+-----------+----------+
Lab Activity:
Students kindly perform the above discussed topics i.e. Order by, Group by, Having Clauses and
Wild cards in your lab sessions and also the below given questions.
4.The following SQL statement lists the number of customers in each country, sorted high to low (Only include
countries with more than 5 customers).
Constraints
SQL Constraints
Constraints are the rules enforced on data columns on a table. These are used to limit the type of data that can go into
a table. This ensures the accuracy and reliability of the data in the database.
Following are some of the most commonly used constraints available in SQL –
1. NOT NULL Constraint − Ensures that a column cannot have a NULL value.
The NOT NULL constraint enforces a column to NOT accept NULL values.
For example, the following SQL query creates a new table called CUSTOMERS and adds five columns, three of which,
are ID NAME and AGE, In this we specify not to accept NULLs –
);
To create a NOT NULL constraint on the "Salary" column when the "Customers" table is already created, use the
following SQL:
2. DEFAULT Constraint − Provides a default value for a column when none is specified.
The default value will be added to all new records IF no other value is specified.
For example, the following SQL creates a new table called CUSTOMERS and adds five columns. Here, the SALARY column
is set to 5000.00 by default, so in case the INSERT INTO statement does not provide a value for this column, then by
default this column would be set to 5000.00.
);
3. UNIQUE Constraint − Ensures that all the values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns.
However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.
For example, the following SQL query creates a new table called CUSTOMERS and adds five columns. Here, the AGE
column is set to UNIQUE, so that you cannot have two records with the same age.
);
A table can have only one primary key, which may consist of single or multiple fields.
Here is the syntax to define the ID attribute as a primary key in a CUSTOMERS table.
To create a PRIMARY KEY constraint on the "ID" column when the CUSTOMERS table already exists, use the following
SQL syntax −
5. FOREIGN Key Constraint − Uniquely identifies a row/record in any another database table.
A foreign key is a key used to link two tables together. This is sometimes also called as a referencing key.
A Foreign Key is a column or a combination of columns whose values match a Primary Key in a different table.
The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign Key in the second table.
Example
Consider the structure of the following two tables.
CUSTOMERS table
);
ORDERS table
DATE DATETIME,
AMOUNT double,
);
If the ORDERS table has already been created and the foreign key has not yet been set, the use the syntax for specifying
a foreign key by altering a table.
6. CHECK Constraint − The CHECK constraint ensures that all values in a column satisfy certain conditions.
If you define a CHECK constraint on a single column it allows only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns
in the row.
For example, the following program creates a new table called CUSTOMERS and adds five columns. Here, we add a
CHECK with AGE column, so that you cannot have any CUSTOMER who is below 18 years.
);
If the CUSTOMERS table has already been created, then to add a CHECK constraint to AGE column, you would write a
statement like the one given below.
7. INDEX − Used to create and retrieve data from the database very quickly. An Index can be created by using a single
or group of columns in a table.
Indexes are used to retrieve data from the database very fast. The users cannot see the indexes, they are just used to
speed up searches/queries.
Tip: Updating a table with indexes takes more time than updating a table without (because the indexes also need an
update). So, only create indexes on columns that will be frequently searched against.
For example, the following SQL syntax creates a new table called CUSTOMERS and adds five columns in it.
);
Now, you can create an index on a single or multiple columns using the syntax given below.
To create an INDEX on the AGE column, to optimize the search on customers for a specific age, you can use the follow
SQL syntax which is given below −
Joins
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.Different
Types of SQL JOINs are:
• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
• RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
• FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
Student
1. INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long as the condition satisfies.
This keyword will create the result-set by combining all rows from both the tables where the condition satisfies
i.e value of the common field will be same.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Tip: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER JOIN.
This query will show the names and age of students enrolled in different courses.
Output:
2. LEFT JOIN: This join returns all the rows of the table on the left side of the join and matching rows for the table on
the right side of join. The rows for which there is no matching row on right side, the result-set will contain null.
LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
Tip: We can also use LEFT OUTER JOIN instead of LEFT JOIN, both are same.
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Tip: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both are same.
Output:
4. FULL JOIN: FULL JOIN creates the result-set by combining result of both LEFT JOIN and RIGHT JOIN. The result-set
will contain all the rows from both the tables. The rows for which there is no matching, the result-set will
contain NULL values.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
1. CARTESIAN JOIN:
The CARTESIAN JOIN is also known as CROSS JOIN. In a CARTESIAN JOIN there is a join for each row of one table to
every row of another table. This usually happens when the matching column or WHERE condition is not specified.
▪ In the absence of a WHERE condition the CARTESIAN JOIN will behave like a CARTESIAN PRODUCT . i.e., the
number of rows in the result-set is the product of the number of rows of the two tables.
▪ In the presence of WHERE condition this JOIN will function like a INNER JOIN.
▪ Generally speaking, Cross join is similar to an inner join where the join-condition will always evaluate to
True
Syntax:
Output:
2. SELF JOIN:
As the name signifies, in SELF JOIN a table is joined to itself. That is, each row of the table is joined with itself and all
other rows depending on some conditions. In other words we can say that it is a join between two copies of the same
table.
Sometimes is required to join the table to itself. To join a table to itself, “two copies” of the same table have to be
opened in the memory.
• Since the table names are the same, the second table will overwrite the first table. In effect, this will result in
only one table being in memory.
➢ This is because a table name is translated into a specific memory location.
• Hence in the FROM clause, the table name needs to be mentioned twice with an “alias”
➢ These two table aliases will cause two identical tables to be opened in different memory locations.
➢ This will result in two identical tables to be physically present in the computer memory.
Output:
In an Equijoin, the WHERE statement compares two columns from two tables with the equivalence operator “=”. This
JOIN returns all rows from both tables, where there is a match.
Syntax:
• Equi Join which is sometimes also referred to as Inner Join or simple join is done by writing a join condition
using the “=“ operator
• Typically the tables are joint to get meaningful data.
• The join is based on the equality of column values in the two tables and therefore is called an Equijoin.
• To join together “n” tables, you need a minimum of “n-1” JOIN conditions.
To display student code and name along with thedepartment name to which they belong
SELECT Student_Code,Student_name,Dept_name
Example 2:
To display student and staff name along with thedepartment name to which they belong
staff_master.dept_code=department_master.dept_code;
4. NON-EQUI JOIN
Example: To display details of staff_members who receive salary in the range defined as per grade
SELECT s.staff_name,s.staff_sal,sl.grade
FROM staff_master s,salgrade sl
WHERE staff_sal BETWEEN sl.losal and sl.hisal
Name Type
GRADE NUMBER
NUMBER
LOSAL
NUMBER
HISAL
So to display all the staff members who receive salary between the ranges specified in the salgrade table we will use a
non-equijoin.
Lab Activity:
Students kindly perform the above discussed topics in your lab sessions.
Views
SQL | Views
Examples:
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22| MP |4500.00|
+----+----------+-----+-----------+----------+
1. Following is an example to create a view from the CUSTOMERS table. This view would be used to have customer
name and age from the CUSTOMERS table.
FROM CUSTOMERS;
Now, you can query CUSTOMERS_VIEW in a similar way as you query an actual table. Following is an example for the
same.
+----------+-----+
| name | age |
+----------+-----+
| Ramesh | 32 |
| Khilan | 25 |
| kaushik | 23 |
| Chaitali | 25 |
| Hardik | 27 |
| Komal | 22 |
| Muffy | 24 |
+----------+-----+
If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.
The following code block has an example of creating same view CUSTOMERS_VIEW with the WITH CHECK OPTION.
The WITH CHECK OPTION in this case should deny the entry of any NULL values in the view's AGE column, because the
view is defined by data that does not have a NULL value in the AGE column.
3. Updating a View
A view can be updated under certain conditions which are given below −
• All NOT NULL columns from the base table must be included in the view in order for the INSERT query to
function.
So, if a view satisfies all the above-mentioned rules then you can update that view. The following code block has an
example to update the age of Ramesh.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Here, we cannot insert rows in the CUSTOMERS_VIEW because we have not included all the NOT NULL columns in this
view, otherwise you can insert rows in a view in a similar way as you insert them in a table.
This would ultimately delete a row from the base table CUSTOMERS and the same would reflect in the view itself. Now,
try to query the base table and the SELECT statement would produce the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
6. Dropping Views
Obviously, where you have a view, you need a way to drop the view if it is no longer needed. The syntax is very simple
and is given below −
Lab Activity:
Students kindly perform the above discussed topics in your lab sessions.
Functions
What are SQL Functions?
SQL provides many built-in functions to perform operations on data. These functions are useful while performing
mathematical calculations, string concatenations, sub-strings etc. SQL functions are divided into two categories,
1. Aggregate Functions
2. Scalar Functions
Aggregate Functions
These functions return a single value after performing calculations on a group of values. Following are some of the
frequently used Aggregrate functions.
1. AVG() Function:
Average returns average value after calculating it from values in a numeric column.
Its general syntax is,
avg(salary)
8200
2. COUNT() Function
Count returns the number of rows present in the table either based on some condition or without condition.
Its general syntax is,
count(name)
Example of COUNT(distinct):
Consider the following Emp table
count(distinct salary)
3. FIRST() Function:
First function returns first value of a selected column
Syntax for FIRST function is,
first(salary)
9000
4. LAST() Function
LAST function returns the return last value of the selected column.
Syntax of LAST function is,
last(salary)
8000
5. MAX() Function
MAX function returns maximum value from selected column of the table.
MAX(salary)
10000
MIN(salary)
7. SUM() Function
SUM function returns total sum of a selected columns numeric values.
Syntax for SUM is,
41000
Scalar Functions
Scalar functions return a single value from an input value. Following are some frequently used Scalar Functions in SQL.
1. UCASE() Function
UCASE function is used to convert value of string column to Uppercase characters.
Syntax of UCASE,
Result is,
UCASE(name)
ANU
SHANE
ROHAN
SCOTT
TIGER
2. LCASE() Function
LCASE function is used to convert value of string columns to Lowecase characters.
Syntax for LCASE is,
LCASE(name)
Anu
Shane
Rohan
Scott
Tiger
MID(name,2,2)
Ha
Oh
Co
Ig
4. ROUND() Function
ROUND function is used to round a numeric field to number of nearest integer. It is used on Decimal point values.
Syntax of Round function is,
ROUND(salary)
9001
8001
6000
10000
8000
Lab Activity:
Students kindly perform the above discussed topics in your lab sessions.
A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again.
So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to
execute it.
You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter
value(s) that is passed.
EXEC procedure_name;
Below is a selection from the "Customers" table in the Northwind sample database:
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
The following SQL statement creates a stored procedure named "SelectAllCustomers" that selects all records from the
"Customers" table:
Example
EXEC SelectAllCustomers;
The following SQL statement creates a stored procedure that selects Customers from a particular City from the
"Customers" table:
Example
Example
Setting up multiple parameters is very easy. Just list each parameter and the data type separated by a comma as
shown below.
The following SQL statement creates a stored procedure that selects Customers from a particular City with a particular
PostalCode from the "Customers" table:
Example
Lab Activity:
Students kindly perform the above discussed topics in your lab sessions.
The SQL UNION clause/operator is used to combine the results of two or more SELECT statements without returning
any duplicate rows.
UNION
Here, the given condition could be any given expression based on your requirement.
Example
Consider the following two tables.
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22| MP |4500.00|
|7|Muffy|24|Indore|10000.00|
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
|100|2009-10-0800:00:00|3|1500|
|101|2009-11-2000:00:00|2|1560|
|103|2008-05-2000:00:00|4|2060|
+-----+---------------------+-------------+--------+
Now, let us join these two tables in our SELECT statement as follows −
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
| 5 | Hardik | NULL | NULL |
| 6 | Komal | NULL | NULL |
| 7 | Muffy | NULL | NULL |
+------+----------+--------+---------------------+
Syntax
The basic syntax of the UNION ALL is as follows.
UNION ALL
Here, the given condition could be any given expression based on your requirement.
Example
Consider the following two tables,
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|7|Muffy|24|Indore|10000.00|
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
|102|2009-10-0800:00:00|3|3000|
|100|2009-10-0800:00:00|3|1500|
|101|2009-11-2000:00:00|2|1560|
|103|2008-05-2000:00:00|4|2060|
+-----+---------------------+-------------+--------+
Now, let us join these two tables in our SELECT statement as follows −
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION ALL
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Note: UNION only appends distinct values. Whereas UNION ALL produces duplicate rows.
Lab Activity:
Students kindly perform the above discussed topics in your lab sessions.
Indexes
❖ Indexes are special lookup tables that the database search engine can use to speed up data retrieval. Simply
put, an index is a pointer to data in a table. An index in a database is very similar to an index in the back of a
book.
❖ For example, if you want to reference all pages in a book that discusses a certain topic, you first refer to the
index, which lists all the topics alphabetically and are then referred to one or more specific page numbers.
❖ An index helps to speed up SELECT queries and WHERE clauses, but it slows down data input, with
the UPDATE and the INSERT statements. Indexes can be created or dropped with no effect on the data.
❖ Creating an index involves the CREATE INDEX statement, which allows you to name the index, to specify the
table and which column or columns to index, and to indicate whether the index is in an ascending or descending
order.
❖ Indexes can also be unique, like the UNIQUE constraint, in that the index prevents duplicate entries in the
column or combination of columns on which there is an index.
1. Single-Column Indexes
A single-column index is created based on only one table column. The basic syntax is as follows.
2. Unique Indexes
Unique indexes are used not only for performance, but also for data integrity. A unique index does not allow any
duplicate values to be inserted into the table. The basic syntax is as follows.
3. Composite Indexes
A composite index is an index on two or more columns of a table. Its basic syntax is as follows.
Whether to create a single-column index or a composite index, take into consideration the column(s) that you may use
very frequently in a query's WHERE clause as filter conditions.
Should there be only one column used, a single-column index should be the choice. Should there be two or more
columns that are frequently used in the WHERE clause as filters, the composite index would be the best choice.
4. Implicit Indexes
Implicit indexes are indexes that are automatically created by the database server when an object is created. Indexes
are automatically created for primary key constraints and unique constraints.
You can check the INDEX Constraint chapter to see some actual examples on Indexes.
The following guidelines indicate when the use of an index should be reconsidered.
• Indexes should not be used on columns that contain a high number of NULL values.
SQL TRIGGERS
Introduction to SQL Trigger
➢ A SQL trigger is executed or fired whenever an event associated with a table occurs
e.g., insert, update or delete.
➢ A SQL trigger is a special type of stored procedure. It is special because it is not called directly like a stored
procedure.
➢ The main difference between a trigger and a stored procedure is that a trigger is called automatically when a
data modification event is made against a table whereas a stored procedure must be called explicitly.
➢ You can use these triggers on Views, or Tables to perform any of the above specified operations.
➢ Remember, you can associate a trigger to a singletable only.
There are mainly two types of triggers:
1. AFTER TRIGGERS
The after / for triggers will run on trigger after an INSERT, DELETE, or an UPDATEon a table. It means, Before the trigger
start running, all the operations should be executed, and the statement has to succeed the constrain check as well. After
triggers are not supported on Views so, use them on tables only. It can be further divided into
1. AFTER INSERT: This trigger will fire after the completion of Insert operation on table.
2. AFTER UPDATE: This trigger will fire after the Update operation is completed on table.
3. AFTER DELETE: After delete trigger will fire after the completion of Delete operation on Employee table.
The Instead of Triggers are fired before the execution of an INSERT, DELETE, or an UPDATE on a table start. It means,
Before the trigger start running, it does not need any condition check, or constrain check. So, this trigger will execute
even the constrain check fails. It can be further divided into
1. INSTEAD OF INSERT: This trigger will fire before the Insert operation on Employee table starts.
2. INSTEAD OF UPDATE: This update trigger will fire before the updating the records in Employee table
3. INSTEAD OF DELETE: This delete trigger will fire before deleting records from Employee table starts.
1. The basic syntax behind the After Triggers in SQL Server is as shown below:
• Schema_name: Please specify the schema name. For example, dbo, or Humanresource etc.
• Trigger_Name: You can specify any name you wish to give other than the system reserved keywords. Please try
to use meaningful names so that you can identify them easily.
2. The basic syntax behind the Instead Of Triggers in SQL Server is as shown below:
Table: employee
empid empname managerid deptid salary DOB
Table: department
1.Can you get employee details whose department id is not valid or department id not present in department table?
There are multiple ways to do this.
Using NOT IN
set rowcount 1
delete from employee where empid in (
select empid
from employee
group by empid,empname, salary
having count(*)>1
)
set rowcount 0
6. Now, can you find 3rd, 5th or 6th i.e. N'th highest Salary?
Query for 3rd highest salary
7. Can you write a query to find employees with age greater than 30?
select * from employee
where datediff(year,dob, getdate()) >30
8. Write an SQL Query to print the name of the distinct employees whose DOB is between 01/01/1960 to 31/12/1987
select distinct empname from employee
where dob between '01/01/1960' and '12/31/1987'
9. Please write a query to get the maximum salary from each department.
select deptid, max(salary) as salary from employee group by deptid
11. Can you write a query to get employee names starting with a vowel?
Using like operator and expression,
13. Create view of the Employee table having empid, empname, salary columns only.
Create view v_employee
As select empid, empname, salary
From employee;
Then:
Select * from v_employee;
Array
• The values can be field values, calculated values, literals, or a combination thereof.
• The only requirement is that all the values are of the same data type.
• The following is an example of creating an array using the fruit_array table and literals:
Example:
ARRAY_AGG
• The sole aggregation for arrays takes all the values in a column and aggregates them into one field.
• For example
• As with all array functions the values can be field values, calculated values, literals, or a combination thereof.
• The only requirement is that all the values are of the same data type.
ARRAY_CAT
• The ARRAY_CAT function is used to concatenate two arrays.
array1 = [1,2,3]
ARRAY_LOWER
• This function is used to return the lower bound of the requested array dimension.
ARRAY_to_string
• This function is used to concatenate array elements using supplied delimiter and optional null string.
This example shows how to use the array_to_string() function to convert {1, NULL, 2, 1} to a comma-separated string.
Note that the NULL element in the array is ignored. If you want to output the NULL element as a specific value, for
example 0, use the following statement: SELECT array_to_string(ARRAY[1, NULL, 2, 1], ',', '0');
Comment
• Comments can make your application easier for you to read and maintain.
• For example, you can include a comment in a statement that describes the purpose of the statement within your
application.
✓ Any text between -- and the end of the line will be ignored (will not be executed).
--Select all:
SELECT * FROM Customers