DMS UNIT 3
DMS UNIT 3
2) Alter:- To add a new column in the table or modify an existing column in the
table
Syntax:-ALTER TABLE table_name ADD column_name COLUMN-definition;
ALTER TABLE MODIFY(COLUMN DEFINITION....);
Eg. :-Alter table Student add (subject varchar(10));
This statement is used to group records having the same values. The GROUP BY
statement is often used with the aggregate functions to group the results by one or
more columns. Apart from this, the GROUP BY clause is also used with the
HAVING clause and JOINS to group the result set based on conditions.
Here, you can add the aggregate functions before the column names, and also a
HAVING clause at the end of the statement to mention a condition.
Examples:
Example:
Write a query to retrieve the number of employees in each city.
Count(EmpID) City
2 Delhi
2 Mumbai
1 Pune
Example:
Write a query to retrieve the number of employees having different salaries in each
city.
SELECT City, Salary, Count(*) FROM Employees GROUP BY City, Salary;
Output:
The table will have the following data:
When we use the SQL GROUP BY statement with the ORDER BY clause, the
values get sorted either in ascending or descending order.
Example:
Write a query to retrieve the number of employees in each city, sorted in
descending order.
Count(EmpID) City
2 Delhi
2 Mumbai
1 Pune
Example: Write a query to retrieve the number of employees in each city, having
salary > 15000
Count(EmpID) City
2 Delhi
2 Mumbai
1 Pune
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
The INNER JOIN keyword selects records that have matching values in both
tables.
The INNER JOIN keyword selects all rows from both tables as long as there is a
match between the columns. If there are records in the "emp" table that do not have
matches in "dept", these records will not be shown
Emp dept
1 100 HR
2 200 TIS
3 100 HR
The LEFT JOIN keyword returns all records from the left table (emp), even if there
are no matches in the right table (dept).
Output:-
1 100 HR
2 200 TIS
3 500 Null
The RIGHT JOIN keyword returns all records from the right table (table2), and the
matching records from the left table (table1). The result is 0 records from the left
side, if there is no match.
The RIGHT JOIN keyword returns all records from the right table (emp), even if
there are no matches in the left table (dept).
Output:-
empid did dname
1 100 HR
2 200 TIS
Null 50 DEV
The FULL OUTER JOIN keyword returns all records when there is a match in left
(table1) or right (table2) table records.
The FULL OUTER JOIN keyword returns all matching records from both tables
whether the other table matches or not. So, if there are rows in "Dept" that do not
have matches in "emp", or if there are rows in "emp" that do not have matches in
"dept", those rows will be listed as well.
Output:-
1 100 HR
2 200 TIS
3 500 NULL
NULL 50 DEV
A self join is a regular join, but the table is joined with itself.
Mahesh Ganesh
Ganesh Sanjay
Sanjay Sanjay
Sub Queries
• The Subquery or Inner query is an SQL query placed inside another SQL
query. It is embedded in the HAVING or WHERE clause of the SQL
statements.
O/P
Student_RollNo. Stu_Name Stu_Marks Stu_City
1003 Bheem 87 Gurgaon
1004 Chetan 95 Noida
1005 Diksha 99 Agra
1006 Raman 90 Ghaziabad
SQL Operators
Arithmetic Operators
These operators are used to perform operations such as addition, multiplication,
subtraction etc.
Operator Operation Description
+ Addition Add values on either side of the operator
Used to subtract the right hand side value from
– Subtraction
the left hand side value
Multiples the values present on each side of the
* Multiplication
operator
Divides the left hand side value by the right hand
/ Division
side value
Divides the left hand side value by the right hand
% Modulus
side value; and returns the remainder
Comparison Operators
These operators are used to perform operations such as equal to, greater than, less
than etc.
Operator Operation Description
Used to check if the values of both operands are
= Equal to equal or not. If they are equal, then it returns
TRUE.
Returns TRUE if the value of left operand is
> Greater than
greater than the right operand.
Checks whether the value of left operand is less
< Less than
than the right operand, if yes returns TRUE.
Used to check if the left operand is greater than
Greater than or
>= or equal to the right operand, and returns TRUE,
equal to
if the condition is true.
Less than or Returns TRUE if the left operand is less than or
<=
equal to equal to the right operand.
Used to check if values of operands are equal or
<> or != Not equal to
not. If they are not equal then, it returns TRUE.
Not greater Checks whether the left operand is not greater
!>
than than the right operand, if yes then returns TRUE.
SET Operations in SQL
SQL supports few Set operations which can be performed on the table data.
These are used to get meaningful results from data stored in the table, under
different special conditions.
UNION is used to combine the results of two or more SELECT statements.
However it will eliminate duplicate rows from its resultset. In case of union,
number of columns and datatype must be same in both the tables, on which
UNION operation is being applied.
UNION ALL
This operation is similar to Union. But it also shows the duplicate rows.
INTERSECT
Intersect operation is used to combine two SELECT statements, but it only
retuns the records which are common from both SELECT statements. In case
of Intersect the number of columns and datatype must be same.
NOTE: MySQL does not support INTERSECT operator.
MINUS
The Minus operation combines results of two SELECT statements and return
only those in the final result, which belongs to the first set of the result.
String Functions:
Output: database@456
Output: PUNE
INITCAP : This function converts alpha character values to uppercase for the first
letter of each word and all others in lowercase.
Output:15
SUBSTR : This function returns a portion of a string from a given start point to an
end point. If a substring length is not given, then SUBSTR returns all the
characters till the end of string (from the starting position specified).
Output: Manage
LPAD and RPAD : These functions return the strings padded to the left or right (
as per the use ) ;
Ex. SELECT LPAD('100',5,'*') FROM DUAL;
Output: **100
Output: 5000***
TRIM : This function trims the string input from the start or end (or both) If no
string or char is specified to be trimmed from the string and there exists some extra
space at start or end of the string, then those extra spaces are trimmed off.
Output: ujarat
Output: Gujarat
REPLACE : This function searches for a character string and, if found, replaces it
with a given replacement string at all the occurrences of the string.