KKW Unit 3 Interactive SQL and Advanced SQL (Part A)
KKW Unit 3 Interactive SQL and Advanced SQL (Part A)
TRANSLATE(string,fromlist,tolist): Returns string with each character in the fromlist replaced with the
corresponding character in the tolist.
Syntax :Selecttranslate(string, from list, to list) from <table Name>;
Ex :Select translate(„Tick ‟, „Ti‟, „Cli‟)from dual;
O/P :Click
SUBSTR(string, pos, len): Returns the substring of string which begins at pos and is len characters long.
If pos is negative, pos is counted from the end of string. The first position in string is 1 not 0.
Syntax :Select substr(string, pos, len) from <table Name>;
Ex :select substr(„computer‟, 4, 3)from dual;
O/P :put
2. Numeric Functions: Numeric Functions accepts the numeric values as the input and it returns the numeric
value as the output
3. Date and Time Functions: These functions can be applied to columns with data type data and time.
Sysdate variable stores current date of system
Months_between(d1,d2): This function finds the number of months between d1 and d2. If d1 is later than
d2 the result is positive. If d1 is earlier than d2 the result is negative
Syntax: Select Months_between(d1, d2) from <table name>;
Ex :Select months_between(‟05-may-2019‟, ‟05-jan-2019‟)from dual;
O/P :4
Add_months(d,n): It returns the date after adding number of months specified with the function
Syntax: Select add_months(d, n) from <table name>;
Ex :Select add_months(‟05-may-2019‟, 2)from dual;
O/P :05-jul-2019
Next_day(d, char):It returns the date of the first weekday named „char‟ that is later than the date „d‟
Syntax: Select next_day(d, „char‟) from <table name>;
Ex :Select next_day(‟31-aug-2019‟, „Tuesday‟)from dual;
O/P :03-sep-2019
Last_day(d): It returns the last day of the month that contain date d. Used to determine how many days left
in the month
Syntax: Select last_day(d) from <table name>;
Ex :Select last_day(‟10-aug-2019‟)from dual;
O/P :31-AUG-2019
Count(n) or Count(*): count(n) function count the number of rows of n. count(*) count all rows including
duplicates and rows with nulls.
Syntax: SELECT count(column_name)FROM table_name WHERE condition;
Ex: Select count(Price) from product;
O/P:05
Here , Where condition is optional.
PRODUCT_MAST
PRODUCT COMPANY QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
1. GROUP BY Clause
o SQL GROUP BY statement is used to arrange identical data into groups. The GROUP BY statement is used
with the SQL SELECT statement.
o The GROUP BY statement follows the WHERE clause in a SELECT statement and precedes the ORDER
BY clause.
o The GROUP BY statement is used with aggregation function.
Syntax: SELECT column Name, Group Function () FROM <table name >
where <conditions> GROUP BY column name;
Output:
Com1 5
Com2 3
Com3 2
2. HAVING Clause
o HAVING clause is used to specify a search condition for a group or an aggregate.
o Having is used in a GROUP BY clause. If you are not using GROUP BY clause then you can use HAVING
function like a WHERE clause.
Syntax: SELECT column Name, Group Function () FROM <table name >
where <conditions> GROUP BY column name
HAVING conditions ORDER BY column1, column2;
Output:
Com1 5
Com2 3
3. ORDER BY Clause
o The ORDER BY clause sorts the result-set in ascending or descending order.
o It sorts the records in ascending order by default. DESC keyword is used to sort the records in descending
order.
Employee
Project
Project_No Emp_Id Department
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables as long as the condition is satisfied. It
returns the combination of all rows from both the tables where the condition satisfies.
Syntax:
SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Robert Development
Christian Designing
Kristen Development
Syntax: SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 LEFT OUTER JOIN table2
ON table1.matching_column = table2.matching_column;
Ex: Select Employee.Emp_Name, Project.Department From Employee Left Outer Join Project
On Project.Emp_Id = Employee.Emp_Id;
Output:
Emp_Name Department
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
Syntax: SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 RIGHT OUTER JOIN table2
ON table1.matching_column = table2.matching_column;
Ex: Select Employee.Emp_Name, Project.Department From Employee Right Outer Join Project
On Project.Emp_Id = Employee.Emp_Id;
Output:
Emp_Name Department
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Syntax: SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 FULL OUTER JOIN table2
ON table1.matching_column = table2.matching_column;
Ex: Select Employee.Emp_Name, Project.Department From Employee Full Outer Join Project
On Project.Emp_Id = Employee.Emp_Id;
Output:
Emp_Name Department
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
Example
Consider the Employee table have the following records:
Ex: Select * From Employee Where Id In (Select Id from Employee Where Salary > 4500);
This would produce the following result:
Output:
Id Name Age Address Salary
4 Alina 29 UK 6500.00
5 Kathrin 34 Bangalore 8500.00
7 Jackson 25 Mizoram 10000.00
o SQL subquery can also be used with the Insert statement. In the insert statement, data returned from the
subquery is used to insert into another table.
o In the subquery, the selected data can be modified with any of the character, date functions.
Syntax: INSERT INTO table_name (column1, column2, column3....) SELECT * FROM table_name
WHERE VALUE OPERATOR;
Example:
Consider a table EMPLOYEE_BKP with similar as EMPLOYEE. Now use the following syntax to copy the
complete EMPLOYEE table into the EMPLOYEE_BKP table.
Ex: INSERT INTO EMPLOYEE_BKP SELECT * FROM EMPLOYEE WHERE ID IN (SELECT IDFROM EMPLOYEE);
The subquery of SQL can be used in conjunction with the Update statement. When a subquery is used with the
Update statement, then either single or multiple columns in a table can be updated.
Syntax: UPDATE table SET column_name = new_value WHERE VALUE OPERATOR (SELECT COLUMN_NAME
FROM TABLE_NAME WHERE condition);
Example: Let's assume we have an Customers_Bkp table available which is backup of Employee table. The given
example updates the SALARY by .25 times in the EMPLOYEE table for all employee whose AGE is greater than or
equal to 29.
SQL>Update Employee Set Salary = Salary * 0.25 Where Age In (Select Age From
Customers_Bkp Where Age >= 29);
This would impact three rows, and finally, the EMPLOYEE table would have the following records.
The sub query of SQL can be used in conjunction with the Delete statement just like any other statements mentioned
above.
This would impact three rows, and finally, the EMPLOYEE table would have the following records.