0% found this document useful (0 votes)
45 views35 pages

DMA-chapter No2

The document discusses various SQL commands and operators like DML, INSERT, UPDATE, DELETE, comparison operators, logical operators, pattern matching operators, aggregate functions and date functions. It provides syntax and examples for each.

Uploaded by

Kiran Kshirsagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views35 pages

DMA-chapter No2

The document discusses various SQL commands and operators like DML, INSERT, UPDATE, DELETE, comparison operators, logical operators, pattern matching operators, aggregate functions and date functions. It provides syntax and examples for each.

Uploaded by

Kiran Kshirsagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Amrutvahini Polytechnic Sangamner

Program – IF Class – IF4I

Course – Database Management Code – 22416

Staff – Navale N.D


Chapter No 2
Interactive SQL (12 M)
DML commands
• DML commands are mainly used to create
new databases, users, constraints, tables,
constraints, etc. The primary purpose of
DML commands is to select, insert,
deleting, update, and delete data records
DML(Data Manipulation Language):
• DML(Data Manipulation Language): The SQL
commands that deals with the manipulation of data
present in the database belong to DML or Data
Manipulation Language and this includes most of the
SQL statements. Examples of DML:
• INSERT – is used to insert data into a table.
• UPDATE – is used to update existing data within a
table.
• DELETE – is used to delete records from a database
table.
INSERT:
• INSERT: The INSERT statement is a
SQL query. It is used to insert data into
the row of a table.
• Syntax:
1.INSERT INTO TABLE_NAME VALUES (va
lue1, value2, value3, .... valueN);
• For example:
1.INSERT INTO javatpoint (Author, Subje
ct) VALUES ("Sonoo", "DBMS");
UPDATE:
• UPDATE: This command is used to update
or modify the value of a column in the table.
• Syntax:
1.UPDATE table_name SET [column_name1= v
alue1,...column_nameN = valueN] [WHERE C
ONDITION]
• For example:
1.UPDATE students
2.SET User_Name = 'Sonoo'
3.WHERE Student_Id = '3'
DELETE:
• DELETE: It is used to remove one or
more row from a table.
• Syntax:
1.DELETE FROM table_name [WHERE c
ondition];
• For example:
1.DELETE FROM javatpoint
2.WHERE Author="Sonoo";
SQL Operator
SQL Arithmetic Operators

Operator Description Example


+ It adds the value of both a+b will give 30
operands.
- It is used to subtract the a-b will give 10
right-hand operand from the
left-hand operand.

* It is used to multiply the a*b will give 200


value of both operands.

/ It is used to divide the left- a/b will give 2


hand operand by the right-
hand operand.

% It is used to divide the left- a%b will give 0


hand operand by the right-
hand operand and returns
reminder.
SQL Comparison Operators:
Operator Description Example
= It checks if two operands values are equal or not, if the (a=b) is not true
values are queal then condition becomes true.
!= It checks if two operands values are equal or not, if values (a!=b) is true
are not equal, then condition becomes true.
<> It checks if two operands values are equal or not, if values (a<>b) is true
are not equal then condition becomes true.
> It checks if the left operand value is greater than right (a>b) is not true
operand value, if yes then condition becomes true.
< It checks if the left operand value is less than right operand (a<b) is true
value, if yes then condition becomes true.
>= It checks if the left operand value is greater than or equal to (a>=b) is not true
the right operand value, if yes then condition becomes true.
<= It checks if the left operand value is less than or equal to (a<=b) is true
the right operand value, if yes then condition becomes true.
!< It checks if the left operand value is not less than the right (a!=b) is not
operand value, if yes then condition becomes true.
SQL Logical Operators

Operator Description
ALL It compares a value to all values in another
value set.
AND It allows the existence of multiple conditions in
an SQL statement.

ANY It compares the values in the list according to


the condition.
BETWEEN It is used to search for values that are within a
set of values.
IN It compares a value to that specified list value.

NOT It reverses the meaning of any logical


operator.
OR It combines multiple conditions in SQL
statements.
EXISTS It is used to search for the presence of a row
in a specified table.

LIKE It compares a value to similar values using


wildcard operator.
Set operators
• 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.
• In this tutorial, we will cover 4 different types of SET
operations, along with example:
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
UNION Operation
Range Searching Operator-Between
• The SQL BETWEEN condition allows you to easily test if an
expression is within a range of values (inclusive). The values
can be text, date, or numbers. The SQL BETWEEN Condition
will return the records where expression is within the range
of value1 and value2.
• Syntax:
• SELECT column_name(s)FROM table_name WHERE
column_name BETWEEN value1 AND value2;
• Example
• SELECT Fname, Lname FROM Employee WHERE Salary
BETWEEN 30000 AND 45000;
Pattern Matching Operator
• The LIKE operator is used in a WHERE clause
to search for a specified pattern in a column.
• There are two wildcards often used in
conjunction with the LIKE operator:
• The percent sign (%) represents zero, one, or
multiple characters
• The underscore sign (_) represents one, single
character
• LIKE Syntax
• SELECT column1, column2, ...FROM table_name WHERE columnN LIKE pattern;

LIKE Operator Description


WHERE CustomerName LIKE 'a%'Finds any values that start with "a"
WHERE CustomerName LIKE '%a'Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second
position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least
2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least
3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
IN Operator
• The IN operator allows you to specify multiple values in a WHERE clause.
• The IN operator is a shorthand for multiple OR conditions.

• IN Syntax
• SELECT column_name(s)FROM table_nameWHERE column_name IN
(value1, value2, ...);

• Example
• SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
• Try it Yourself »
SQL | String functions

• ASCII(): This function is used to find the ASCII value of a character.


• Syntax: SELECT ascii('t');
• Output: 116

• CHAR_LENGTH(): Doesn’t work for SQL Server. Use LEN() for SQL Server. This function
is used to find the length of a word.
• Syntax: SELECT char_length('Hello!');
• Output: 6

• CHARACTER_LENGTH(): Doesn’t work for SQL Server. Use LEN() for SQL Server. This
function is used to find the length of a line.
• Syntax: SELECT CHARACTER_LENGTH('geeks for geeks');
• Output: 15

• CONCAT(): This function is used to add two words or strings.


• Syntax: SELECT 'Geeks' || ' ' || 'forGeeks' FROM dual;
• Output: ‘GeeksforGeeks’
• LCASE(): This function is used to convert the given string into lower case.
• Syntax: LCASE ("GeeksFor Geeks To Learn");
• Output: geeksforgeeks to learn

• LEFT(): This function is used to SELECT a sub string from the left of given
size or characters.
• Syntax: SELECT LEFT('geeksforgeeks.org', 5);
• Output: geeks

• LENGTH(): This function is used to find the length of a word.


• Syntax: LENGTH('GeeksForGeeks');
• Output: 13
• LOWER(): This function is used to convert the upper case string into lower case.
• Syntax: SELECT LOWER('GEEKSFORGEEKS.ORG');
• Output: geeksforgeeks.org

• LPAD(): This function is used to make the given string of the given size by adding
the given symbol.
• Syntax: LPAD('geeks', 8, '0');
• Output:
• 000geeks

• LTRIM(): This function is used to cut the given sub string from the original
string.
• Syntax: LTRIM('123123geeks', '123');
• Output: geeks
• REPLACE(): This function is used to cut the given string by removing the given sub
string.
• Syntax: REPLACE('123geeks123', '123');
• Output: geeks
• REVERSE(): This function is used to reverse a string.
• Syntax: SELECT REVERSE('geeksforgeeks.org');
• Output: ‘gro.skeegrofskeeg’
• RIGHT(): This function is used to SELECT a sub string from the right end of the given
size.
• Syntax: SELECT RIGHT('geeksforgeeks.org', 4);
• Output: ‘.org’
• RPAD(): This function is used to make the given string as long as the given size by
adding the given symbol on the right.
• Syntax: RPAD('geeks', 8, '0');
• Output: ‘geeks000’
Date and Time Function- My-SQL

Function Description
SYSDATE Returns the current date and time
CURDATE() Returns the current date
CURTIME() Returns the current time
DATE() Extracts the date part of a date or date/time
expression
EXTRACT() Returns a single part of a date/time
DATE_ADD() Adds a specified time interval to a date
DATE_SUB() Subtracts a specified time interval from a date
DATEDIFF() Returns the number of days between two dates
DATE_FORMAT() Displays date/time data in different formats
Date and Time Function- Oracle
Aggregate Function
• AVG() - Returns the average value
• COUNT() - Returns the number of rows
• FIRST() - Returns the first value
• LAST() - Returns the last value
• MAX() - Returns the largest value
• MIN() - Returns the smallest value
• SUM() - Returns the sum
Math Function
• Name Description
• ABS Returns the absolute value
• EXP Returns the e constant (2.71828…) that raises to a power of a specified
number
• LOG Returns the natural logarithm of the first argument
• MOD Returns the remainder (modulo) of a number divided by another
• PI Returns the value of pi which is 3.14159265358979
• POWER Returns a number raised to a power of a specified number
• RAND Returns a random floating-point value
• ROUND Rounds a number to a specific precision
• SIGN Returns the sign of an argument
• SIN Returns the sine of an argument
• SQRT Returns the square root of an argument
• TAN Returns the tangent of an argument
• TRUNCATE Truncates to a specified number of decimal places
Where clause
SQL GROUP BY Clause

• The GROUP BY statement groups rows that


have the same values into summary rows, like
"find the number of customers in each
country".

• The GROUP BY statement is often used with


aggregate functions (COUNT(), MAX(), MIN(),
SUM(), AVG()) to group the result-set by one
or more columns.
• GROUP BY Syntax
• SELECT aggregate
function,column_name(s)FROM table_name
WHERE condition GROUP BY column_name(s);
• Example
• SELECT COUNT(CustomerID), Country
FROM Customers GROUP BY Country;
SQL HAVING Clause

• The HAVING clause was added to SQL because


the WHERE keyword cannot be used with
aggregate functions.

• HAVING Syntax
• SELECT column_name(s) FROM table_name
WHERE condition GROUP BY column_name(s)
HAVING condition;
• Example
• SELECT COUNT(CustomerID), Country FROM
Customers GROUP BY Country HAVING
COUNT(CustomerID) > 5;
SQL ORDER BY

• 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 Syntax
• SELECT column1, column2, ...FROM table_name ORDER
BY column1, column2, ... ASC|DESC;
• Example
• SELECT * FROM Customers
• ORDER BY Country;
SQL JOIN
• SQL JOIN
• A JOIN clause is used to combine rows from
two or more tables, based on a related
column between them.
OrderID CustomerID OrderDate
10308 2 1996-09-18 Custome Custome ContactN Country
rID rName ame
10309 37 1996-09-19
1 Alfreds Maria Germany
10310 77 1996-09-20 Futterkist Anders
e
2 Ana Ana India
Trujillo Trujillo
Empared
ados y
helados
TCL
1. TCL(transaction Control Language): TCL commands
deal with the transaction within the database.
Examples of TCL commands:
1. COMMIT– commits a Transaction.
2. ROLLBACK– rollbacks a transaction in case of any error
occurs.
3. SAVEPOINT–sets a savepoint within a transaction.
4. SET TRANSACTION–specify characteristics for the
transaction.

You might also like