Unit 3 (1)
Unit 3 (1)
1. String Function:
Function Description
REPLACE Replaces all occurrences of a substring within a string, with a new substring
2.REPLACE(): The REPLACE() function replaces all occurrences of a substring within a string, with
a new substring.
To get the ceiling or nearest rounded up value of 17.36 from the student table , the following SQL
statement can be used :
Example: SELECT(CEIL(17.36))
FROM student;
Output: (CEIL(17.36))-------------18
2.FLOOR(X)
This returns the largest integer value that is either less than X or equal to X.
Syntax:FLOOR(expression)
Example:SELECT FLOOR(17.36)
FROM dual;
Output:FLOOR(17.36)------------17
3.ROUND(X)
This function returns the value of X rounded off to the whole integer that is nearest to it.
Syntax: Select Round(Expression);
Example: select Round(5.7)
Output: 6
Example: select Round(5.2)
Output: 5
4. SQRT(X)
This function returns the square root of X. For example −
Syntax:select sqrt(expression)
Example: Select sqrt(9)
Output: 3
Example: Select sqrt(49)
Output: 7
The following table lists the most important built-in date functions in MySQL:
Function Description
Syntax: NOW()
Example:SELECT NOW(),CURDATE(),CURTIME()
Output: 2023-09-13
Syntax: CURTIME()
Output:09:45:34
Time Function:The TIME() function extracts the time part from a given time/datetime.
Syntax: TIME(expression)
Example:SELECT TIME("2017-08-15 19:30:10");
3. Aggregate Functions:
An aggregate function in SQL returns one value after calculating multiple values of a column. We often
use aggregate functions with the GROUP BY and HAVING clauses of the SELECT statement.
Count()
Sum()
Avg()
Min()
Max()
1. COUNT FUNCTION: COUNT function is used to Count the number of rows in a database table. It
can work on both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.
Suppose, you want to count the total values of the Emp_City column
2. SUM Function: Sum function is used to calculate the sum of all selected columns. It works on
numeric fields only.
Syntax: SELECT SUM (column_name) FROM table_name;
ID EMPLOYEE_NAME SALARY
1 Saurabh 25000
2 Ram 29000
3 Sumit 30000
4 Ankit 45000
If you want to know how the combined total salary of all employee
Totalsalary
1,29,000
3. AVG function: The AVG function is used to calculate the average value of the numeric type.
AVG function returns the average of all non-Null values.
Syntax: SELECT AVG(column_name) FROM table_name;
Product_ID Product_Name Product_Quantity
104 P1 10.250
202 P4 15.500
103 P2 18.250
111 P7 25.250
210 P6 15.500
212 P8 19.750
Average_ofproductquantity
16.39
4. MAX Function:MAX function is used to find the maximum value of a certain column. This
function determines the largest value of all selected values of a column.
ID EMPLOYEE_NAME SALARY
1 Saurabh 25000
2 Ram 29000
3 Sumit 30000
4 Ankit 45000
SALARY
450000
5. MIN Function: MIN function is used to find the minimum value of a certain column. This
function determines the smallest value of all selected values of a column.
ID EMPLOYEE_NAME SALARY
1 Saurabh 25000
2 Ram 29000
3 Sumit 30000
4 Ankit 45000
SALARY
25000
GROUP BY Syntax:
SELECT column_name(s)
FROM table_name
GROUPBY column_name(s);
mysql
Joins: SQL Join statement is used to combine rows from two or more tables based on a common field
between them.
Inner Join:The INNER JOIN keyword selects all rows from both the tables as long as the
condition is satisfied.
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 the same.
Syntax: SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Department
Dept_Id Dept_Name
1001 Finance
1002 Marketing
1003 Sales
1004 Coding
1005 Administration
Employee_Detail
It only fetches the details of those employees from both the tables whose Dept_Id in the
Employee table matches with the Dept_Id of the Department table.
Output:
JOIN UNION
It combines data into new columns. It combines data into new rows
The number of columns selected from The number of columns selected from each table
each table may not be the same. should be the same.
The FULL OUTER JOIN command returns all rows when there is a match in either left table or right
table.
Syntax:SELECT column_name(s)
FROM table1FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
Example:
SELECT students.name, books.title
FROM students FULLOUTERJOIN books
ON students.student_id=books.student_id;
we are selecting the names from the students table and the book titles from the books table.
Records are matched using the student_id column in both tables.
With the full outer join, we are able to see all of the students, including those who have not
chosen a book yet. We can also see all of the books, including those that have not yet been
chosen.
Output:
What is the Difference between INNER JOIN vs OUTER JOIN in SQL?
The biggest difference between an INNER JOIN and an OUTER JOIN is that the inner join will
keep only the information from both tables that's related to each other (in the resulting table). An
Outer Join, on the other hand, will also keep information that is not related to the other table in the
resulting table.
Group By Clause:GROUP BY statement groups rows that have the same values into summary ...
The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , ...
From Table_name
Group by Column_name(s);
Example:Emp
Output:
E_Name Sum(Salary)
Geeta 2000
Ram 1000
Sham 3000
Order By Clause:The ORDER BY keyword is used to sort the result-set in ascending or descending
order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.
Order by Salary;
Example:
Output:
Having Clause: This clause is used in SQL because we cannot use the WHERE clause with the SQL
aggregate functions. Both WHERE and HAVING clauses are used for filtering the records in SQL
queries.
The HAVING clause places the condition in the groups defined by the GROUP BY clause in the
SELECT statement.
From table_name
Group By column_name
Having condition;
Ex Query:Select name,sum(salary)
from emp
group by E_name
having sum(salary)>20000;
Example: Emp
Output:Emp
E_Name Sum(Salary)
Ram 50000
sham 30000
Create View: In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database.
You can add SQL statements and functions to a view and present the data as if the data were
coming from one single table.
A view is created with the CREATE VIEW statement.
Output: Emp1
E_Id Salary
1 10000
2 30000
3 40000
4 20000
Update View: Update View refers to a view (logic) to update a particular instance of a table from the
database with some extra details. It is used to update entries in the database
Set col1=value,col2=value2…..
Where condition;
Set salary=60000
Where id = 2;
Result: Query OK
Output: emp1
Drop View:The SQL DROP VIEW statement is used to delete an existing view, along with its
definition and other information. Once the view is dropped, all the permissions for it will also be
removed.
And note that if a record is deleted from a view, it is also deleted from its corresponding base table.
Syntax:DROPVIEW view_name;
Delete View: Instead of removing an entire view we can also delete selected rows of a view using the
DELETE statement.
Output: Emp1
Sequences:
STARTWITH1
INCREMENT BY1
MINVALUE 1
MAXVALUE 5
CYCLE;
In the above query, the sequence is named “My_Sequence” and it starts with the value 1 and
increments by 1 each time a value is generated. The sequence has a maximum value of 5 and cycles
back to the
Output: SQL> SELECT * FROM STUDENTS;
+------+-----------+------+
| ID | NAME | AGE |
+------+-----------+------+
| 1 | Dhruv | 20 |
| 2 | Arjun | 23 |
| 3 | Dev | 25 |
| 4 | Riya | 19 |
| 5 | Aarohi | 24 |
| 1 | Lisa | 20 |
| 2 | Roy | 24 |
+------+-----------+------+
Left Join:This join returns all the rows of the table on the left side of the join and matches rows for
the table on the right side of the join. For the rows for which there is no matching row on the right
side, the result-set will contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
Course
Ex Query:SELECT Student.NAME,Course.COURSE_ID
FROM StudentLEFT JOIN Course
ON Course.ROLL_NO = Student.ROLL_NO;
Output:
Right Join: RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the
right side of the join and matching rows for the table on the left side of the join. For the rows for
which there is no matching row on the left side, the result-set will contain null. RIGHT JOIN is also
known as RIGHT OUTER JOIN.
Syntax:SELECT table1.column1,table1.column2,table2.column1,....
FROM table1 RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
//Consider same tables as student and course//
Ex Query: SELECT Student.NAME,Course.COURSE_ID
FROM Student RIGHT JOIN Course
ON Course.ROLL_NO = Student.ROLL_NO;
Output:
Index: The Index in SQL is a special table used to speed up the searching of the data in the database
tables. It also retrieves a vast amount of data from the tables frequently. The INDEX requires its own
space in the hard disk.
The index concept in SQL is same as the index concept in the novel or a book.
Create Index: The CREATE INDEX statement is used to create indexes in tables.
Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see
the indexes, they are just used to speed up searches/queries.
Syntax
There are various types of indexes that can be created using the CREATE INDEX statement. They are:
Unique Index
Single-Column Index
Composite Index
Implicit Index
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. It is automatically created by PRIMARY and
UNIQUE constraints when they are applied on a database table, in order to prevent the user from
inserting duplicate values into the indexed table column(s). The basic syntax is as follows.
A single-column index is created only on one table column. The syntax is as follows.
A composite index is an index that can be created on two or more columns of a table. Its basic syntax is
as follows.
DROP INDEX
An index can be dropped using SQL DROP command. Dropping an index can effect the query
performance in a database. Thus, an index needs to be dropped only when it is absolutely necessary.
Synonym: An alias or alternative names can be given to any of the database objects like a table, view,
stored procedure, user-defined function, and sequence with the help of SQL Server Synonym.
Whenever we create a SQL Server Synonym in a database, the synonym is referenced to a particular
database object and that database object is called base object. The location of the base object to which
the synonym is referenced can be either in the same database or in some other database in the same
server or even on some other instance running on another server.
Input Description
<synonym>The name of the synonym.
<name> The name of the table or view for which the synonym is to be created.
Drop Synonym: Use the DROP SYNONYM statement to remove a synonym from the database or to
change the definition of a synonym by dropping and re-creating it.