SQL
SQL
Database Concepts
• Data is a collec)on of characters, numbers, and other symbols that represents values of
some situa)ons or variables. Data – plural; Datum – singular.
• A Database Management System (DBMS) is a so>ware system that enables users to define,
create, maintain and control access to the database. Example: MySQL, Oracle, MongoDB.
• Data Model refers to the model used for organizing or storing data in a database.
o Rela5onal Data Model organizes data as a collec)on of two-dimensional
inter-related tables called rela)ons.
o Hierarchical Data Model organizes data into a tree-like structure with each record
(collec)on of fields) having a single parent. A record can have any number of child
records.
o Network Data Model organizes data into a network of inter-linked records (i.e.,
organized like a graph) with no restric)ons on the number of links a record can have.
o Object-Oriented Data Model organizes data into inter-related objects that mirror
objects in object-oriented programming languages.
Relational Data Model
The Relational Data Model organizes data as a collection of two dimensional inter related tables
called relations.
• Rela5on – A collec)on of tuples
• Tuple – A tuple is a set of aQribute values for the aQributes in a rela)on
• AEribute – The column/field of a table/rela)on is known as an aQribute.
• Domain – The domain of an aQribute is the set of permissible values that the aQribute can
take.
• Degree – Number of aEributes in the rela)on.
• Cardinality – Number of tuples in the rela)on.
A database that is modelled on rela)onal data model concept is called Rela5onal Database. A DBMS
that works with rela)onal databases is called a Rela5onal Database Management System (RDBMS).
• Candidate Key – An aQribute (or a combina)on of aQributes) that uniquely iden)fies every
tuple in a rela)on is a Candidate Key of the rela)on.
• Primary Key – The Primary Key of a rela)on is an aQribute (or a combina)on of aQributes)
that uniquely iden)fies every tuple in a rela)on (i.e., a Candidate Key) and is the key that is
designated as the most appropriate one to uniquely iden)fy every tuple in the rela)on.
• Alternate Key – Alternate Keys are all the Candidate Keys of a rela)on that are not the
Primary Key.
• Foreign Key
SQL (MySQL)
Introduction
Structured query language (SQL) is a programming language for storing and processing information
in a relational database.
SQL is the standard query language used across all RDBMSs. The level of support for SQL by different
RDBMS implementations may vary.
MySQL is an open source RDBMS that is developed, distributed, and supported by Oracle
Corporation. MySQL uses SQL as the query language.
Official Website
https://www.mysql.com/
MySQL GitHub
https://github.com/mysql
Data Types
n <= m
Note:
• Unsigned numeric types have UNSIGNED added to the types name. For example,
INT UNSIGNED is an unsigned integer.
• Integer types and DECIMAL are called exact value types while FLOAT and DOUBLE are called
approximate value types.
String Data Types
Literals
• Numeric Literals
o Exact value literals
§ Integers. The data type of the literal will be INT if the number can fit in the
INT data type. For numbers that cannot fit in INT, larger data types are used
in the following order BIGINT, BIGINT UNSIGNED, DECIMAL.
Examples: 1, 0, 191, -5
• Date values are wriQen as strings in the format 'YYYY-MM-DD'. Note that these are string
literals that get automa)cally converted to DATE.
• String literals
Note: TRUE and FALSE pre-defined constants that evaluate to 1 and 0 respectively. They are
technically not literals.
Operators
Following is a subset of SQL operators in order of precedence. Parentheses can be used to override
precedence. Among infix operators with same precedence, le>-to-right associa)vity applies.
IN Membership check
NOT IN
IS NULL Null check
IS NOT NULL
Logical not. 1 if operand is zero 0 if it is non-
7 NOT zero.
• x DIV y is calculated as truncate(x/y). – here the truncate opera)on discards the frac)onal
part. Example: –10.1 DIV 3 is –3.
• Data type DIV return value depends on the data type of operands but the result will always
be an integer value.
• x MOD y is calculated as x - (x DIV y) * y. Example: –10.1 MOD 3 is –1.1.
• x MOD y, if non-zero, will have the sign of the x (1st operand).
• % is same as MOD.
In a division opera)on expr1 / expr2 where expr1 and expr2 are exact value numeric types the data
type of the result will have a scale (i.e., no. of digits a>er decimal point) that is four more than the
scale of expr1. The scale of expr2 has no effect on the scale of the result. Note that the increased
scale will not be larger than 30. The value of the result will be Examples:
• Division operators (DIV MOD / %) give NULL as result when right side operand is zero.
• expr IS NOT NULL will evaluate to true (i.e., 1) if expr is not NULL
• Following rules apply if one or more expressions is NULL in expr IN (expr1, expr2, …).
o If expr is NULL the result will be NULL.
o If expr is not NULL and is present in the list, result will be true (i.e., 1).
o If expr is not NULL & not present in the list and the list has one or more NULLs, the
result will be NULL.
• Following rules apply if one or more expressions is NULL in expr NOT IN (expr1, expr2, …).
o If expr is NULL the result will be NULL.
o If expr is not NULL and is present in the list, result will be false (i.e., 1).
o If expr is not NULL & not present in the list and the list has one or more NULLs, the
result will be NULL.
• The result of <=> (NULL-safe equal to operator) will never be NULL. Following rules apply if
one or more operands of <=> is NULL.
o If both operands are NULL, result is true (i.e., 1).
o If only one of the operands is NULL, result is false (i.e., 0).
• Except for the above rules the result of all other operators will be NULL when any of the
operands is NULL.
Comments
#…………………..
Begins with # and ends with new-line.
-- …………………..
Begins with -- followed by a space and ends with new-line.
/*…………………..*/
Begins with /* and ends with */ can span mul)ple lines.
Statements
Statement Syntax
Keywords – Words that have special meaning in MySQL. Keywords cannot be used for any user
defined names like table names, column names etc. Example of keywords: CREATE, SELECT, INSERT,
etc.
Statements (or Commands) – Instruc)ons given to the database to perform some ac)on.
Clauses – A statement consists of one or more logically dis)nct parts called clauses. Clauses begin
with a keyword which is followed by arguments.
Rule of thumb: All CREATE, ALTER and DROP statements are DDL.
Note: TRUNCATE is works as a DDL command though it only deletes data without changing
the schema. Just to be safe – don’t give TRUNCATE as an example for DDL. Men)on
TRUNCATE only when it is really required.
Rule of thumb: All INSERT, UPDATE, DELETE and SELECT statements are DML.
Database Commands
• SHOW DATABASES;
• USE database_name; -- sets the default database for subsequent commands. Note: This
is also referred to as “opening the database”, though it is technically not the case.
• SHOW TABLES;
• DESCRIBE table_name;
• DESC table_name; -- same as DESCRIBE
Constraints
Constraints ensure database integra)ng. Also called Database Integrity Constraints. Constraints
define certain restric)ons for the data that can be stored in the tables.
Types of Constraints:
NOT NULL and DEFAULT constraints can be defined only as column constraints.
PRIMARY KEY, UNIQUE, FOREIGN KEY and CHECK can be defined as both column constraints and
table constraints.
CREATE TABLE Statement
• Columns that are part of the PRIMARY KEY cannot be specified as nullable.
• If NULL or NOT NULL is not explicitly specified, NOT NULL is assumed for primary key
columns and NULL is assumed for other columns.
Note: Explicitly specifying nullable columns is redundant and causes no change in the
behaviour.
• A primary key column can be added only if the table does not already have a primary key.
• All exis)ng records get the DEFAULT value as the value for the newly added column. For
nullable columns with DEFAULT value, all rows get NULL as the value for the newly added
column.
• When a TIME or numeric NOT NULL column is added without DFAULT value, all exis)ng
records get zero as the value for the newly added column.
• When a string NOT NULL column is added without DFAULT value, all exis)ng records get
empty string as the value for the newly added column.
• Adding a DATETIME/TIMESTAMP NOT NULL column without DFAULT value to a table already
containing rows will cause an error.
Note that the list of column defini)ons is enclosed in parentheses, as in CREATE TABLE.
Removing a column
An op)onal COLUMN keyword can be added (for readability) – DROP COLUMN instead of DROP.
Modify a column
Primary key can be added only if the table does not already have a primary key.
Adding Unique Constraint
INSERT Statement
Inser)ng by giving values for all columns in create order. Values should be provided for all columns in
the table.
Inser)ng by giving values some of the columns in the table. The remaining columns will take the
default values of the respec)ve columns. This syntax should be used when the order of the columns
is not known.
INSERT statement will fail if the provided values result in a viola)on of any constraint.
SELECT Statement
When the list of columns specified is a subset of the columns in the table, the opera)on is called a
Projec5on.
Example.
Employee Table:
Name Salary
Tom 10000
Harry 16000
John NULL
MaQ 11000
Peter 10000
Mary NULL
SELECT Name FROM Employee WHERE Salary < 15000 will return Tom, MaE & Peter.
SELECT Name FROM Employee WHERE Salary > 15000 will return Harry.
SELECT Name FROM Employee WHERE Salary BETWEEN 11000 AND 15000 will return Harry & MaE.
SELECT Name FROM Employee WHERE Salary IS NULL will return John & Mary.
Column aliasing
Here Employee and "Monthly Salary" are column aliases. A column alias is an alternate name given
to an item in the select list.
The AS keyword is op)onal. The below query is same as the above one.
SELECT Name EName, Salary "Monthly Salary" FROM Employee ORDER BY EName;
SELECT Name EName, Salary "Monthly Salary" FROM Employee ORDER BY Name;
DISTINCT clause
Will return
Salary
10000
16000
NULL
11000
Student table.
Name Mark1 Mark2
Tom 60 70
Harry 80 70
John NULL 80
MaQ NULL 80
Peter 70 NULL
Mary NULL NULL
Victor NULL NULL
Will return:
Mark1 Mark2
60 70
80 70
NULL 80
70 NULL
NULL NULL
ORDER BY clause
Will return:
Name Mark1 Mark2
Mary NULL NULL
Victor NULL NULL
John NULL 80
MaQ NULL 80
Tom 60 70
Peter 70 NULL
Harry 80 70
The ordering is in ascending order by default. DESC op)on should be used for descending order:
Will return:
Note:
• NULL values come first in ascending order and last in descending order.
• When DISTINCT is used along with ORDER BY, all columns used in ORDER BY should be in the
select list. This rule prevents unpredictable ordering of the results.
DUAL is a keyword refers to a dummy table name. The FROM DUAL at the end is op)onal.
Examples:
SELECT NOW();
SELECT 100 + 5;
Note that syntac)cally all SELECT statement clauses like WHERE, GROUP BY etc., can be used in this
kind of SELECT as well. However, those clauses are meaningless in this context.
SELECT ALL
ALL a>er the SELECT keyword is to indicate that all records are returned (instead of DISTINCT). This is
the behaviour when neither ALL not DISTINCT is men)oned. ALL keyword is just for readability – it
does not change the seman)cs of the SELECT statement.
Example:
Is same as
UPDATE Statement
UPDATE table_name
SET column1 = expr1,
column2 = expr2, …
WHERE condi)on;
Example:
Cau)on: If the WHERE clause is missed, the updates will occur for all records in the table.
DELETE Statement
Example:
Cau)on: Similar to UPDATE, if the WHERE clause is missed, all records in the table will get deleted.
Functions
Function Types
• Scalar Func5ons or Single Row Func5ons – These func)ons take scalar values as argument
and returns a scalar value. Examples:
o Numeric (Math) func)ons
o String func)ons
o Date & Time func)ons
• Aggregate Func5ons or Mul5ple Row Func5ons or Group Func5ons - These func)ons work
on a set of records as a whole and return a single value.
Scalar Functions
Numeric (Math) functions
• ABS(n)
• POWER(x,y) / POW(x,y)
• ROUND(n) / ROUND(n,d)
• FLOOR(n)
• CEIL(n)
• TRUNCATE(n,d)
Examples:
Func5on Call Result
ABS(-5.7) 5.7
POWER(3,2) 9
POWER(0.09,0.5) 0.3
POWER(-1,0.5) Error
POWER(-1,0.2) Error
POWER(-25,-2) 0.0016
POWER(0,0) 1
ROUND(24.5) 25
ROUND(-24.5) -25
ROUND(24.2345, 3) 24.235
ROUND(-24.2345, 3) -24.235
ROUND(145.5, -1) 150
TRUNCATE(24.23458, 4) 24.2345
TRUNCATE(-24.23458, 4) -24.2345
TRUNCATE(27.5, 0) 27
TRUNCATE(-27.5, 0) -27
TRUNCATE(64.23458, -1) 60
TRUNCATE(64.23458, -2) 0
Note:
• Raising nega)ve number to a frac)onal power causes error
• If any argument is null for a func)on, the result is NULL.
Current Date & Time function
NOW() – Current date & )me – the )me at which the current statement started execu)ng.
NOW(n) – Current date & )me including frac)onal seconds up to n digits. 0 <= n <= 6. Default is zero.
IFNULL function
IFNULL(v1, v2)
Aggregate functions
Examples:
Student table:
Name Subject Marks
Tom Math 76
Tony Math 66
Matt Math 87
Peter Math 56
Tom Physics 46
Tony Physics 71
Matt Physics 70
Peter Physics 71
Tom Chemistry 66
Tony Chemistry 51
Matt Chemistry 56
Mary Chemistry NULL
Query Result
SELECT COUNT(*) FROM Student 12
SELECT COUNT(Marks) FROM Student 11
SELECT COUNT(DISTINCT Marks) FROM Student 8
SELECT MAX(Marks) FROM Student 87
SELECT MIN(Marks) FROM Student 46
SELECT SUM(DISTINCT Marks) FROM Student 523
SELECT SUM(Marks) FROM Student 716
SELECT AVG(Marks) FROM Student 65.0909
SELECT AVG(DISTINCT Marks) FROM Student 65.3750
SELECT COUNT(*) FROM Student WHERE Marks >= 70 5
SELECT COUNT(Marks) FROM Student WHERE Marks >= 70 5
Note:
• DISTINCT op)on is available for MAX() and MIN() but it has no impact on the result.
• ALL op)on can be specified like SUM(ALL Marks). Here, ALL indicates that all values are
included (opposite of DISTINCT). This is the default behaviour if neither ALL nor DISTINCT is
men)oned. ALL is just for readability purpose - it does not change the meaning of the
aggregate func)on call. Examples MAX(ALL Marks), COUNT(ALL *), etc.
Result:
TopMark
98
GROUP BY clause
Aggregate func)ons by default perform the aggrega)on taking all records got a>er applying the
WHERE clause condi)on. The result is always a single row that has the results of the aggrega)on.
GROUP BY clause is used when it is required to split the records in the table into mul)ple groups and
apply the aggrega)on on each group of records separately. For example, let’s say, with the above
Student table, the requirement is to find the subject-wise averages instead of the overall average.
First the records need to be split into different groups based on the subject (as shown below) and
then the averages need to be calculated for each group (i.e., each subject).
Subject Average
Math 71.2500
Physics 64.5000
Chemistry 57.6667
Similarly,
Will give the average for each student. The result will be:
Name Average
Tom 62.6667
Tony 62.6667
Matt 71.0000
Peter 63.5000
Mary NULL
Records with NULL in the GROUP BY column are put in a group of its own. For example, let’s say the
Student table has the following two addi)onal rows.
Subject Average
Math 71.2500
Physics 64.5000
Chemistry 57.6667
NULL 45.5000
Sales table:
State City SalesDate SalesAmount
Karnataka Bangalore 2023-08-07 10000
Karnataka Mysore 2023-08-07 7000
Karnataka Mangalore 2023-08-07 6000
Tamil Nadu Chennai 2023-08-07 12000
Tamil Nadu Coimbatore 2023-08-07 11000
Tamil Nadu Trichy 2023-08-07 9000
Karnataka Bangalore 2023-08-08 11000
Karnataka Mysore 2023-08-08 8000
Karnataka Mangalore 2023-08-08 5000
Tamil Nadu Chennai 2023-08-08 13000
Tamil Nadu Coimbatore 2023-08-08 19000
Tamil Nadu Trichy 2023-08-08 8000
Tamil Nadu NULL 2023-08-07 6500
Tamil Nadu NULL 2023-08-08 5000
NULL NULL 2023-08-07 7500
NULL NULL 2023-08-08 6000
HAVING clause
A>er the records are split into groups (by GROUP BY) further condi)ons can be defined at the group
level. This is done using the HAVING clause. Example:
Student table:
Name Subject Marks
Tom Math 76
Tony Math 66
Matt Math 87
Peter Math 56
Tom Physics 46
Tony Physics 71
Matt Physics 70
Peter Physics 71
Tom Chemistry 66
Tony Chemistry 51
Matt Chemistry 56
Mary Math 70
Mary Physics NULL
Mary Chemistry NULL
Here we have marks for three subjects. But all three marks are available only for some students
(Tom, Tony and MaQ). The requirement is to calculate the total marks for all students for whom all
three marks are available. It can be done as below.
SELECT Name, SUM(Marks) Total FROM Student GROUP BY Name HAVING COUNT(Marks) = 3
If the HAVING clause is not included, the result will include incorrect par)al totals as shown below.
Name Total
Mary 70
Tom 188
Peter 127
Tony 188
Matt 213
Group Expressions
A Group-Expression is expression that can be evaluated at the group level. It can contain:
1. Columns whose values are same for all rows in the group as per group defini)on (i.e.,
columns included in GROUP BY clause)
2. Column whose value is restricted to a single value by WHERE clause.
3. Aggregate func)ons.
Examples:
Let’s say the Student table contains columns Name, Subject, InternalMarks & ExternalMarks and
let’s say a SELECT statement is of the form:
The following are valid group-expressions in the context of the above SELECT statement:
SUM(InternalMarks)
AVG(InternalMarks)
MAX(InternalMarks) – MIN(InternalMarks)
MAX(InternalMarks) + ExternalMarks)
Name
The following are non-group-expressions in the context of the above SELECT statement:
InternalMarks + ExternalMarks
InternalMarks - AVG(InternalMarks)
Subject
CONCAT(Name, '-', Subject)
When a SELECT statement has aggregate func)ons or a GROUP BY clause: The select list, HAVING
clause and ORDER BY clause can contain only group-expressions. Examples:
Aggregate func)ons can be used only in the select lists, HAVING clauses and ORDER BY clauses. It
cannot be used in WHERE clauses. This is because WHERE clause is processed before grouping is
done by GROUP BY. For the same reason columns referred to by WHERE clause is not restricted by
the GROUP BY clause.
WHERE, GROUP BY, HAVING and ORDER BY clauses are op)onal but need to be in the above order
when specified.
The above steps are to logically understand what the SELECT statement means. SQL is a
non-procedural language that specifies only what needs to be done and not how it needs to be done.
So, the RDBMs will choose the most efficient way to execute SELECT statement and produce the
result that matches the meaning of the SELECT statement.
Joins
Cartesian product on two tables results in a table having all combina)ons of rows from the
underlying tables. Cartesian product will include all the columns from both the tables. The degree of
the cartesian product will be the sum of the degrees of the two tables and the cardinality will be the
product of the cardinali)es of the two tables.
Example: The following tables give a sports team’s opponent-wise and ground-wise past performance.
PerfByOpponent table
Opponent WinPercent
Australia 40
West Indies 53
New Zealand 53
England 56
PerfGround table
Ground GroundWinPercent
Bengaluru 71
Chennai 54
Delhi 64
Mumbai 55
The requirement is to find the win probability for each Opponent-Ground carbonata)on. This can be
done using cartesian product with any of the following two SELECT commands.
Equi-Join
Equi-join operation
A join opera)on combines rows from two tables on specified condi)ons. If the condi)on based on
which the rows in one table are joined to rows in another table is equal-to, then the join is called
Equi-join.
Product table:
ProductId Name
1 Shirt
2 Pant
3 Tie
ProductPrice table:
ProductId ProductSize Price
1 S 500
1 M 550
1 L 600
2 S 600
2 M 650
2 L 700
3 M 100
3 L 120
Here each product has different price based on size. The requirement to display prices of all the
products for all the available sizes. This needs an equi-join. It can be done in two ways.
Note that wherever the ProductId column is referred it is qualified by the table name. This is done
because both the tables involved in the join have a column named ProductId. Leaving out the
qualifica)on will cause an error.
Table Aliases
Similar to column aliases, tables can also be given aliases. This will be useful when there are mul)ple
tables involved and column names referred to exist in two or more tables. For example:
However, the below SQL is valid because Product.ProductId refers to the un-aliased Product table.
Qualifica)on is not needed for columns that do not have ambiguity. Example:
Note: If a table is joined to itself, tables alias is a must as the joined tables have duplicate names.
Natural Join
NATURAL JOIN joins tables by matching columns with same name in the two tables. This is also an
equi-join where the equal-to condi)on is implied. Example:
Result:
The result is same as earlier except that it has only one ProductId column. This is because with
NATURAL JOIN only one of the two columns matched is kept in the result.
NATURAL JOIN will match rows only if the joined tables have one or more columns with the same
name. If there is no common column name between the tables, NATURAL JOIN will give the
Cartesian product as the result.
Examples: The following SELECT statements get the prices only for 'Shirt'.
Let’s say the is an OrderItem table with columns OrderId, ItemNum, ProductId, ProductSize and
Quan5ty.
Now, the following SELECT statements will join based on two columns (ProductId and ProductSize).
Note:
• The matched columns come first in the order they appear in the le>-side table.
• Remaining columns in the le>-side table come next.
• Remaining columns in the right-side table come last.
If three tables need to be joined, two joins will be required. In general (N-1) joins are needed to join
N tables.
Non-equi-joins
Non-equi-joins are joins that have join condi)ons other than equal-to.
Example:
Sales table
StoreId SaleDate Amount
1 2013-09-01 10000
1 2013-09-02 14000
2 2013-09-01 11000
2 2013-09-02 13000
2 2013-09-03 13000
Inventory table
StoreId InventoryDate Value
1 2013-09-01 41000
1 2013-09-02 24000
1 2013-09-03 43000
2 2013-09-01 31000
2 2013-09-02 33000
The requirement is to list, for all stores, dates on which sales was less than 25% of the inventory. The
below SQL will get the required result.
In the above join, the join condi)on S.Amount < I.Value * 0.25 is not an equal-to condi)on.
So, this is a non-equi-join query.
StoreId SaleDate
1 2013-09-01
§ Date
o 'YYYY-MM-DD'
o 'YY-MM-DD'
o 'YYYYMMDD'
o 'YYMMDD'
§ Time (ffffff represents one or more frac)onal seconds digits. Value is rounded to 6 digits if
more than 6 digits are specified)
o 'hh:mm:ss'
o 'hh:mm:ss.ffffff'
o 'hhmmss'
o 'hhmmss.ffffff'
When colon is used to separate )me parts mm and ss can be one or two digits with value
less than 60. hh can be up to 3 digits and can also have a minus sign.
§ Date & Time (all the below formats can op)onally include frac)onal seconds as above)
o 'YYYY-MM-DD hh:mm:ss'
o 'YY-MM-DD hh:mm:ss'
o 'YYYYMMDDhhmmss'
o 'YYMMDDhhmmss'
• Date
o YYYYMMDD (8 digit number)
o YYMMDD (6 digit number)
• Time
o hhmmss (6 digit number)
o hhmmss.ffffff
When year is specified with two digits (YY), year is taken as 2000 + YY if YY is less than 70.
Else it is taken as 1900 + YY.
DOUBLE Same as FLOAT but without any rounding. More significant digits (up to 17) will
get printed as DOUBLE is larger in size than FLOAT.
Implicit Conversions
All implicit conversions between numeric, string, date, )me and boolean are allowed in MySQL. The
only conversions that can result in error are conversions to date and )me types.
String/numeric to date/)me/date-)me
String/numeric values are converted to date/)me/date-)me values as per the formats described in
Date & Time Literal Formats sec)on. Invalid string/numeric will cause error.
Conversion to string
Done as described in MySQL Client Output Formats sec)on with one excep)on:
• Float values are not rounded when conver)ng
Strings to Numbers
The longest possible valid numeric prefix of the string is used. Examples.
Boolean to Numbers
• For numeric columns, frac)onal values are rounded based on the number of decimals the
column can store.
• If the value provided is too big to fit into the column, an error occurs.
Conversions by Mathematical Operators
• When one of the operands is a FLOAT or DOUBLE, both operands are converted to DOUBLE.
• Else if one operand is a DECIMAL, the other is converted to DECIMAL and the data type of
the result will have size large enough to hold the result of the opera)on.
• If the two operands are of compa)ble types, then the comparison happens without any type
conversion:
• Else, if one of the operands is numeric or boolean, the other operand is converted to
numeric and a numeric comparison is done.
• Else, one operand is a string and the other is TIME. The string is converted to )me and a )me
comparison is done. Results are unpredictable (see note below) if the string is not a valid
)me.
• Else, one operand is a string and the other is DATE/DATETIME/TIMESTAMP. The string is
converted to date-)me and a date-)me comparison is done. Results are unpredictable (see
note below) if the string is not a valid date-)me.
• If all three operands are of compa)ble type (as earlier), the comparison occurs without any
type conversion.
• Else, if one of the operands are a mix of numeric, string and TIME, numeric and string
operands are converted to )me and a )me comparison is done. Results are unpredictable
(see note below) if a string or numeric operand is not a valid )me interval.
• Else, if the operands include at least one DATE/TIME/DATETIME/TIMESTAMP then numeric
and string operands are converted to date-)me and a date-)me comparison is done. For
TIME operands, today’s date is added to get date-)me value. Results are unpredictable (see
note below) if a string or numeric operand is not a valid date-)me.
• Else, the operands are a mix of numeric and strings. All operands are converted to numeric
and a numeric comparison is done.
The condi)on expressions in WHERE clauses, HAVING clauses are converted to numeric and
interpreted as below:
Unpredictable behaviour
It is complicated to explain when each of the above will happen. For exam purpose, it is safe to say
that such conversions will result in an error.
More Scalar Functions
String functions
• UCASE(s) / UPPER(s)
• LCASE(s) / LOWER(s)
• LENGTH(s)
• LEFT(s,n)
• RIGHT(s,n)
• LTRIM(s)
• RTRIM(s)
• TRIM(s)
• INSTR(s1, s2) – Find string in string.
Examples:
Note:
• MID() uses nega)ve indices for coun)ng from the last character in the string. -1 posi)on is
the last character, -2 to is the second last, and so on.
• MID() returns empty string if the posi)on is outside the string range. The posi)on should be
non-zero and within -(string length) to +(string length) to get a non-empty result.
• INSTR() is case insensi5ve.
• INSERT() does not perform any inser)on if posi)on is not posi)ve or higher than the string
length.
• CHAR() returns binary string if the encoding is not specified with USING. The binary string is
printed in hexadecimal form.
• If any argument is null for a func)on, the result is NULL.
• DATE()
• YEAR()
• MONTH()
• DAY()
• TIME() – Returned value will be of TIME type
• MONTHNAME()
• DAYNAME()
Examples:
Note:
• If any argument is null for a func)on, the result is NULL.
NOW() vs SYSDATE()
• NOW() – Current date & )me – the )me at which the current statement started execu)ng.
Returned value will be of DATETIME type.
NOW(n) – Current date & )me including frac)onal seconds up to n digits. 0 <= n <= 6. Default
is zero.
• SYSDATE() & SYSDATE(n) – Similar to NOW() but returns the )me at which the SYSDATE() call
executed. NOW() return value does not change during the execu)on of the current
statement while SYSDATE() can change.
Examples:
Note:
• Argument cannot be NULL for NOW()/SYSDATE().
• The below statement shows the NOW() and SYSDATE() values before sleeping for 0.01
seconds and again a>er sleeping.
The results of the above statement will show that both NOW() calls return the same value
but that is not the case with SYSDATE(). Sample output:
+-------------------------+-------------------------+-------------+-------------------------+-------------------------+
| NOW(3) | SYSDATE(3) | SLEEP(0.01) | NOW(3) | SYSDATE(3) |
+-------------------------+-------------------------+-------------+-------------------------+-------------------------+
| 2023-08-07 16:05:15.667 | 2023-08-07 16:05:15.667 | 0 | 2023-08-07 16:05:15.667 | 2023-08-07 16:05:15.681 |
+-------------------------+-------------------------+-------------+-------------------------+-------------------------+
Note: The blow INSERT will succeed even if course with Id 100 does not exist.
This can be prevented by adding the below foreign key to the Sessions table.
CHECK Constraint
DEFAULT value can be an expression but needs to be enclosed in parenthesis. The default value
expression can refer to other columns in the table but there should be no circular reference.
Example:
The default behaviour is to prevent update of the referenced column in the referenced (or
parent/primary) table.
Example:
With the above tables and data, the following UPDATE fail because the RollNum 101 is referenced by
a Result table row.
UPDATE Student SET RollNum = 104 WHERE RollNum = 101
This default behaviour of UPDATE can be changed by using an ON UPDATE clause as below when
defining the FOREIGN KEY in the Result table.
The earlier UPDATE statement will succeed when ON UDATE CASCADE clause is added to the
FOREIGN KEY defini)on. The resul)ng data will be as below.
Student table data:
RollNum Name
104 Adam
102 Tom
103 Mary
No)ce that the update occurs in the parent table and one or more records in the child table.
There are more ways to handle updates. Below are the different ON UPDATE op)ons possible.
• CASCADE – All child rows are updated with the new parent column value
• SET DEFAULT – Available in standard SQL but ignored by MySQL. This op)on sets the values
in child rows to the default value of the column.
• SET NULL – Sets the values in child rows to NULL. In the above example, Result table will be
as below a>er the update.
Note that in MySQL only CASCADE and SET NULL will affect the UPDATE behaviours, other op)ons
will have no effect.
Simila to the ON UPDATE clause to control UPDATE opera)ons the ON DELETE clause can be used to
control DELETE opera)ons.
Example:
ON DELETE and ON UPDATE clauses are independent of each other – each one can specify a different
behaviour.
The result returned by SELECT can be used to create a new table using.
Example:
This creates ResultCopy table with the same column defini)ons as in Result table and copies all the
rows from Result to ResultCopy.
A Column in the new table has NOT NULL constraints set if the column directly refers to a NOT NULL
column or is an expression involving only NOT columns.
A Column in the new table has DEFAULT value set if it directly refers to a column with a DEFAULT
value.
PRIMARY KEY, UNIQUE, FOREIGN KEY and CHECK constraints are not set in the new table.
MySQL assigns internally generated names to UNIQUE, FOREIGN KEY and CHECK constraints. Note
that no name is internally assigned form PRIMARY KEY constraint. It is possible to explicitly give
names to constraints. Names can be given to UNIQUE, FOREIGN KEY, CHECK and PRIMARY KEY
constraints by using the CONSTRAINT clause.
Example:
Here the CHECK constraint is given the name Chk_Mark. The name can be used to refer to the
constraint when required – for example in an ALTER TABLE statement. Both the following statements
have the same effect.
However, CHECK constraints defined along with the column defini)on can be given names. The
following statement is valid.
Views
A view is a virtual table that does not hold data but derives its data from other tables through a
SELECT query.
A>er the SubjectAverage is created, it can be used just like a table, For example:
SELECT * FROM SubjectAverage
Renaming a column
This works the same way as MODIFY with the addi)on of changing the column name.
AFTER can be used in the same way with MODIFY and CHANGE.
Note that column posi)on cannot be specified when adding mul)ple columns using a single ALTER
statement.
MySQL assigns internally generated names to UNIQUE, FOREIGN KEY and CHECK constraints. Note
that no name is internally assigned form PRIMARY KEY constraint. It is possible to explicitly give
names to constraints. Names can be given to UNIQUE, FOREIGN KEY, CHECK and PRIMARY KEY
constraints by using the CONSTRAINT clause.
Example:
Here the CHECK constraint is given the name Chk_Mark. The name can be used to refer to the
constraint when required – for example in an ALTER TABLE statement. Both the following statements
have the same effect.
UNIQUE, FOREIGN KEY and PRIMARY KEY constraints defined along with the column defini)on
cannot be given a name. The following statement will cause error.
However, CHECK constraints defined along with the column defini)on can be given names. The
following statement is valid.
Mul)ple rows can be inserted with a single INSERT statement by providing mul)ple value sets.
Example:
• The column counts of the inserted table should match the select result.
• The column name of the inserted table should match the column names in the select result.
INSERT INTO ResultCopy(RollNum) SELECT RollNum FROM Result WHERE Marks > 90;
A>er checking that the column count and names match, MySQL maps columns by posi)on not name.
For example:
Result table:
RollNum Marks
1 95
2 85
ResultCopy table:
RollNum Marks
1 95
2 85
95 1
85 2
Set Operations
Union (∪) – Combines the tuples of two selected rela)ons into a single rela)on.
Intersec5on (∩) – Gets the tuples that are present in both the selected rela)ons.
Minus (–) – Also called set difference. Gets the tuples that are present in the first rela)on but not in
the second rela)on.
Duplicates are eliminated in all the set opera)ons. The degrees of the two rela)ons should match for
the set opera)ons to work.
Example:
Faculty table:
Name Mobile
Oswald 9287862627
Derek 9730786996
Grace 9409368878
Henry 9487205331
Oswald 9287862627
Kevin 9458616553
Kevin 9458616553
Tom 9458616556
Student table:
Name Mobile
Steven 9060282540
Thomas 9005103559
Adam 9219569682
Kevin 9458616553
Helen 9828874297
Thomas 9005103559
Tom 9458616556
Union:
SELECT * FROM Faculty
UNION
SELECT * FROM Student;
Name Mobile
Oswald 9287862627
Derek 9730786996
Grace 9409368878
Henry 9487205331
Kevin 9458616553
Tom 9458616556
Steven 9060282540
Thomas 9005103559
Adam 9219569682
Helen 9828874297
Intersec)on:
SELECT * FROM Faculty
INTERSECT
SELECT * FROM Student;
Name Mobile
Kevin 9458616553
Tom 9458616556
Minus:
SELECT * FROM Faculty
EXCEPT
SELECT * FROM Student;
Name Mobile
Oswald 9287862627
Derek 9730786996
Grace 9409368878
Henry 9487205331
Outer joins
The results of the joins discussed so far will not include rows that do not have a matching row
sa)sfying the join condi)on.
For example, let us take the Sales and Inventory tables from the earlier example and run the
following query, which calculates the percentage of inventory sold.
SELECT
IFNULL(S.StoreId, I.StoreId) AS StoreId,
IFNULL(S.SaleDate, I.InventoryDate) AS Date,
S.Amount As Sales,
I.Value As Inventory,
Amount * 100 / Value AS SalePercent
FROM Sales S JOIN Inventory I
ON S.StoreId = I.StoreId AND
S.SaleDate = I.InventoryDate
Note that for Store Id 2 and date 2013-09-03, there is no Inventory table row. Similarly, for Store Id 1
and date 2013-09-03, there is no Sales table row. Since these rows do not have matching rows, they
are not included in the result. This type of join is called an inner join. The default join is inner join. It
can also be explicitly specified on the query as INNER JOIN instead of just JOIN.
In the inner join, the fact that some data is missing is not reflected in the result. To include
unmatched rows an outer join needs to be used. Outer joins are joins that return the matched rows
as well as the unmatched rows from either or both tables.
Le| outer join includes unmatched rows from the le>-side table in the result. Example:
SELECT
#### select columns same as above
FROM Sales S LEFT OUTER JOIN Inventory I
#### join condition same as above
Returns
StoreId Date Sales Inventory SalePercent
1 2013-09-01 10000 41000 24.3902
1 2013-09-02 14000 24000 58.3333
2 2013-09-01 11000 31000 35.4839
2 2013-09-02 13000 33000 39.3939
2 2013-09-03 13000 NULL NULL
Note that the result includes unmatched rows from Sales table (the le>-side table).
Right outer join includes unmatched rows from the right-side table in the result. Example:
SELECT
#### select columns same as above
FROM Sales S RIGHT OUTER JOIN Inventory I
#### join condition same as above
Returns:
StoreId Date Sales Inventory SalePercent
1 2013-09-01 10000 41000 24.3902
1 2013-09-02 14000 24000 58.3333
1 2013-09-03 NULL 43000 NULL
2 2013-09-01 11000 31000 35.4839
2 2013-09-02 13000 33000 39.3939
Note that the result includes unmatched rows from Inventory table (the right-side table).
Full outer join includes unmatched rows from both the joined tables in the result. MySQL does not have
direct support for full outer join – it can be done by doing a union of le> and right outer joins. Example:
SELECT
#### select columns same as above
FROM Sales S LEFT OUTER JOIN Inventory I
#### join condition same as above
UNION
SELECT
#### select columns same as above
FROM Sales S RIGHT OUTER JOIN Inventory I
#### join condition same as above
Returns
StoreId Date Sales Inventory SalePercent
1 2013-09-01 10000 41000 24.3902
1 2013-09-02 14000 24000 58.3333
2 2013-09-01 11000 31000 35.4839
2 2013-09-02 13000 33000 39.3939
2 2013-09-03 13000 NULL NULL
1 2013-09-03 NULL 43000 NULL
The keyword OUTER is op)onal as in case of INNER. So, LEFT OUTER JOIN is same as LEFT JOIN and
RIGHT OUTER JOIN is same as RIGHT JOIN. The ON clause must be present for LEFT or RIGHT joins.
If there join is based on matching column names, NATURAL LEFT JOIN or NATURAL LEFT JOIN can be
used. Note that the ON clause is implied in case of natural join.