UNIT 5-sql
UNIT 5-sql
Natural Join
Natural Join
This join is used to combine rows of tables based on columns having the
same name and data type in both the tables.
The common columns only appear once in the result of this join.
Natural join can be used to combine two or more tables,
Additional joins like LEFT, RIGHT, etc. can be combined with natural
join but usually not with inner join.
syntax :
SELECT Column_1,Column_2,…..Column_n
FROM table_1
If you want to perform a natural join on the entire table you can use the
following syntax:
SELECT * FROM table_1
For example,
It is an INNER JOIN by default that returns only matching rows between the
tables.
Tables are joined based on all columns with the same name and data types. The
SQL engine automatically detects these common columns.
Output contains unique columns; common join columns appear only once.
ON or USING clauses cannot be used to specify join columns, as join uses all
common column names implicitly.
Can be combined with left, right, and full outer joins for more flexibility.
Cartesian Join
Syntax:
ON clause:
SELECT column_list
FROM table1
JOIN table2
ON table1.column_name = table2.column_name;
ANY means that the condition will be true if the operation is true for any of the values
in the range.
Example:
SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
ALL means that the condition will be true only if the operation is true for all values in
the range.
Example
SELECT ALL ProductName
FROM Products
WHERE TRUE;
SELECT ProductName
FROM Products
WHERE ProductID = ALL
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
Subquery
A subquery is a SQL query nested inside a larger query.
o - A SELECT clause
o - A FROM clause
o - A WHERE clause
o - A HAVING clause
A subquery is also called an inner query or inner select, while the statement
containing a subquery is also called an outer query or outer select.
FROM employees;
FROM (SELECT first_name, salary FROM employees WHERE salary > 5000) AS
"high_salaried"
In the WHERE Clause : Used to filter the results.
SELECT first_name
FROM employees
FROM employees
GROUP BY department_id
Type of Subqueries
Correlated Subquery:
Depends on the outer query and is executed for each row processed by the
outer query.
SELECT first_name
FROM employees e1
WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE
e1.department_id = e2.department_id);
ADDDATE(DATE,DAYS)
The numbers of days in integer form (DAYS) is added to the specified date. This is
the value returned by the function. For example −
sql> SELECT ADDDATE('2018-08-01', 31);
+---------------------------------------------------------+
| DATE_ADD('2018-08-01', INTERVAL 31 DAY) |
+---------------------------------------------------------+
| 2018-09-01 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
ADDTIME(exp1,exp2)
This function adds the two expressions exp1 and exp2 and displays the result. In this
case, exp1 can be a datetime or time expression and exp2 is a time expression. For
example:
sql> SELECT ADDTIME('2018-08-01 23:59:59.999999','1 1:1:1.000002');
+---------------------------------------------------------+
|ADDTIME('2018-08-01 23:59:59.999999','1 1:1:1.000002') |
+---------------------------------------------------------+
| 2018-08-02 01:01:01.000001 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
CURDATE():
This returns the current date of the system in the YYYY-MM-DD format.
For example −
sql> SELECT CURDATE();
+---------------------------------------------------------+
| CURDATE() |
+---------------------------------------------------------+
| 2018-08-01 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
CURTIME()
This returns the current time of the system from the current time zone in the format
HH:MM:SS.
For example −
sql> SELECT CURTIME();
+---------------------------------------------------------+
| CURTIME() |
+---------------------------------------------------------+
| 10:56:35 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
This function returns the current time i.e ‘10:56:35’
DAYNAME(date)
For the given date, this function returns the corresponding day of the week.
For example −
sql> SELECT DAYNAME('2018-08-01');
+---------------------------------------------------------+
| DAYNAME('2018-08-01') |
+---------------------------------------------------------+
| Wednesday |
+---------------------------------------------------------+
1 row in set (0.00 sec)
For the date '2018-08-01’, this function returns the day of the week i.e. Wednesday.
DAYOFMONTH(date)
For the given date, it returns the day of the month the date is on. The value of day of
the month ranges from 1 to 31.
For example −
sql> SELECT DAYOFMONTH('2018-02-15');
+---------------------------------------------------------+
| DAYOFMONTH('2018-02-15') |
+---------------------------------------------------------+
| 15 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
This returns the day of the month '2018-02-15' falls on i.e 15.
DAYOFWEEK(date)
For the given date, it returns the day of the week the date is on. The value of day of
the week ranges from 1 to 7 (Sunday is 1 and Saturday is 7). For example −
sql> SELECT DAYOFWEEK('2018-02-15');
+---------------------------------------------------------+
|DAYOFWEEK('2018-02-15') |
+---------------------------------------------------------+
|5|
+---------------------------------------------------------+
1 row in set (0.00 sec)
This returns the day of the week '2018-02-15' falls on i.e 5.
DAYOFYEAR(date)
For the given date, it returns the day of the year the date is on. The value of day of the
year ranges from 1 to 366. For example −
sql> SELECT DAYOFYEAR('2018-02-15');
+---------------------------------------------------------+
| DAYOFYEAR('2018-02-15') |
+---------------------------------------------------------+
| 46 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
This returns the day of the year '2018-02-15' falls on i.e 46.
MONTH(date)
It returns the month value for the corresponding date. The range of the month is from
1 to 12.
For example −
sql> SELECT MONTH('2018-08-01');
+---------------------------------------------------------+
| MONTH('2018-08-01') |
+---------------------------------------------------------+
|8|
+---------------------------------------------------------+
1 row in set (0.00 sec)
This returns the month number for '2018-08-01' which is 8.
TIME(expr)
This function displays the time part of a time or date time expression in the form of a
string.
For example −
sql> SELECT TIME('2018-08-01 11:33:25');
+---------------------------------------------------------+
| TIME('2018-08-01 11:33:25') |
+---------------------------------------------------------+
| 11:33:25 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
This displays the time part of '2018-08-01 11:33:25' in the form of a string.
CURTIME()
This returns the current time of the system from the current time zone in the format
HH:MM:SS.
For example −
sql> SELECT CURTIME();
+---------------------------------------------------------+
| CURTIME() |
+---------------------------------------------------------+
| 10:56:35 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
This function returns the current time i.e ‘10:56:35’
DAYNAME(date)
For the given date, this function returns the corresponding day of the week. For
example −
sql> SELECT DAYNAME('2018-08-01');
+---------------------------------------------------------+
| DAYNAME('2018-08-01') |
+---------------------------------------------------------+
| Wednesday |
+---------------------------------------------------------+
1 row in set (0.00 sec)
For the date '2018-08-01’, this function returns the day of the week i.e. Wednesday.
DAYOFMONTH(date)
For the given date, it returns the day of the month the date is on. The value of day of
the month ranges from 1 to 31.
For example −
sql> SELECT DAYOFMONTH('2018-02-15');
+---------------------------------------------------------+
| DAYOFMONTH('2018-02-15') |
+---------------------------------------------------------+
| 15 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
This returns the day of the month '2018-02-15' falls on i.e 15.
DAYOFWEEK(date)
For the given date, it returns the day of the week the date is on. The value of day of
the week ranges from 1 to 7 (Sunday is 1 and Saturday is 7).
For example −
sql> SELECT DAYOFWEEK('2018-02-15');
+---------------------------------------------------------+
|DAYOFWEEK('2018-02-15') |
+---------------------------------------------------------+
|5|
+---------------------------------------------------------+
1 row in set (0.00 sec)
This returns the day of the week '2018-02-15' falls on i.e 5.
DAYOFYEAR(date)
For the given date, it returns the day of the year the date is on. The value of day of the
year ranges from 1 to 366.
For example −
sql> SELECT DAYOFYEAR('2018-02-15');
+---------------------------------------------------------+
| DAYOFYEAR('2018-02-15') |
+---------------------------------------------------------+
| 46 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
This returns the day of the year '2018-02-15' falls on i.e 46.
MONTH(date)
It returns the month value for the corresponding date. The range of the month is from
1 to 12. For example −
sql> SELECT MONTH('2018-08-01');
+---------------------------------------------------------+
| MONTH('2018-08-01') |
+---------------------------------------------------------+
|8|
+---------------------------------------------------------+
1 row in set (0.00 sec)
This returns the month number for '2018-08-01' which is 8.
TIME(expr)
This function displays the time part of a time or date time expression in the form of a
string.
For example −
sql> SELECT TIME('2018-08-01 11:33:25');
+---------------------------------------------------------+
| TIME('2018-08-01 11:33:25') |
+---------------------------------------------------------+
| 11:33:25 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
This displays the time part of '2018-08-01 11:33:25' in the form of a string.
SQL - String Functions
String functions are functions in SQL that take string values as inputs.
The output may or may not be a string.
SQL offers many in-built string functions
ASCII(str)
BIN(N)
CHARACTER_LENGTH(str)
Syntax- CHARACTER_LENGTH(str)
This function is the same as CHAR_LENGTH(str)
CONCAT(str1,str2,...)
Syntax- CONCAT(str1,str2,...)
This function takes multiple strings as inputs and then concatenates the strings to
get a single string value.
For Example:-
SELECT CONCAT(‘one ‘, ‘line ‘, ‘below’). This will return the string with the
text ‘one line below’.
CONCAT_WS(separator,str1,str2,...)
Syntax- CONCAT_WS(separator,str1,str2,...)
A separator is passed as the first argument, and the separator is added between all
the strings to be concatenated.
For example:- CONCAT_WS(',' , 'John’ , ‘Smith'). This will return the string with
the text ‘John,Smith).
INSTR(str,substr)
Syntax- INSTR(str,substr)
It gives the position of the string substr in the string str.
INSTR(‘abcdefgh’,2,3,’red’)
The returned value is ‘aredefgh’.
INSERT(str,pos,len,newstr)
Syntax- INSERT(str,pos,len,newstr)
It converts the str argument into a new string by adding the string newstr with
the len length and from pos position.
SQL query:-
INSERT(‘abcdefgh’,2,3,’red’)
HEX(N_or_S)
Syntax- HEX(N_or_S)
It converts the integer argument into a hexadecimal representation.
For the below SQL query:-
HEX(255)
FORMAT(X,D)
Syntax- FORMAT(X,D)
It formats the number X to a decimal number rounded off to D decimal
places. The output returned is in the form of a string.
For the below SQL query:-
FORMAT(12345.55553234,4)
FIND_IN_SET(str,strlist)
Syntax- FIND_IN_SET(str,strlist)
It returns the index of the position str in the list of a single string.
For the below SQL query:-
FIND_IN_SET(‘a’,’b,a,c,d’)
FIELD(str,str1,str2,str3,...)
Syntax- FIELD(str,str1,str2,str3,...)
It returns the index of the position str in the arguments. The arguments are
counted starting from 1.
For the below SQL query:-
Field(‘ab’,’abcd’,’ab’,’bfd’,’fdd’)
EXPORT_SET(bits,on,off[,separator[,number_of_bits]])
Syntax- EXPORT_SET(bits,on,off[,separator[,number_of_bits]])
This function uses the argument ‘bits’ to return the on bits and off bits.
For the below SQL query:-
Export_Set(9,’ON’,’OFF’, ‘,’,4)
The string returned is ON,OFF,OFF,ON
ELT(N,str1,str2,str3,...)
Syntax- ELT(N,str1,str2,str3,...)
If the integer is 1, the first string is given as the output.
For Example:- SELECT ELT(2,’abc’,’def’,’ghi’,’jkl’). The output will be the
second string, i.e. ‘def’.
CONV(N,from_base,to_base)
Syntax- CONV(N,from_base,to_base)
For example:- SELECT CONV('a',16,2). This will return the string with the text
‘1010’.