Unit 3 Introduction to SQL
Unit 3 Introduction to SQL
INTRODUCTION
TO SQL
INTRODUCTION TO SQL:
•It allows the user to perform operations such as create, delete etc.
on tables in database
•It also allows operations such as read, insert, update, delete etc. on
the data stored in the tables.
•Portable language: SQL is a portable language, which means the software that
supports SQL can be moved to another machine without affecting the capability of
SQL interacting with the database on new machine.
•SQL is secure.
Advantages of SQL
1. Fast Response Time
You can quickly retrieve large amount of data from database using SQL. The
response time of a SQL query is very fast.
2. Requires No coding
Learning SQL is easy and doesn’t require any prior coding or programming
knowledge. The syntax of SQL is very simple and close to english so learning
curve is smooth.
3. Portable
SQL is portable, it supports various operating system and devices. SQL
statements can be stored as saved procedures and these procedures can be used
on a different machine to perform the same task without needing to rewrite the
statements again.
4. Standardised language
SQL is been used for over the years and has wide variety of well maintained
documentation. This language is so standardised that the same syntax can be
used on various different platforms.
5. Intergation
SQL server can connect to third party backends like Oracle, IDM Db2, MySQL
etc. using drivers. These drivers allow the smooth integration.
6. Secure
SQL allows to set permissions in tables, this makes it secure as a user with no
permission cannot read, write or modify the data in database. SQL also has the
concept of constraints that ensures what type of data can be inserted into the
tables. All these features make SQL a secure database language.
7. Scalable
It is easy to add or drop tables in the database using SQL. Also, the database size
doesn’t affect the performance of SQL that much and it works pretty great with
large databases as well. It is easy to add several new tables into the database as
well as drop multiple tables from database.
SQL Commands
EX.
ALTER TABLE EMPLOYEES
ADD Address VARCHAR2(200);
d) TRUNCATE: This statement deletes all the rows from the table.
This is different from the DROP command, the DROP command deletes
the entire table along with the table schema, however TRUNCATE just
deletes all the rows and leaves an empty table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEES;
RENAME TABLE
In some situations, database administrators and users want to change the
name of the table in the SQL database because they want to give a more
relevant name to the table.
Syntax of RENAME
RENAME old_table _name To new_table_name ;
Ex. RENAME Employee To Coding_Employees ;
2.DML :
The DML commands in Structured Query Language change the data
present in the SQL database.
Following are the four main DML commands in SQL:
SELECT Command
INSERT Command
UPDATE Command
DELETE Command
If we want to retrieve the data from all the columns of the table, we
have to use the following SELECT command:
If you want to access all the records of those students whose marks is
80 from the above table, then you have to write the following DML
command in SQL:
SELECT * FROM Student WHERE Stu_Marks = 80;
2.INSERT Command :
UPDATE command is used to allows users to update or modify the existing data in
database tables.
DELETE Command:
DELETE command is used to remove single or multiple existing
records from the database tables.
WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Syntax:
SELECT column1, column2 FROM table_name WHERE conditions
GROUP BY column1, column2
HAVING conditions
Example:
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>2;
logical operators:
Logical operators are used to specify conditions in the structured query
language (SQL) statement.
They are also used to serve as conjunctions for multiple conditions in a
statement.
The different logical operators are shown below −
LIKE − It is used to compare values to a list of literal values that are specified.
“%” character is used to match any substring and “_” character is used to match
any character. It expresses a pattern by using the ‘like’ comparison operator.
For example,
To display all names whose second letter is ‘b’, use the below mentioned
command −
select * from emp where ename LIKE ‘_b%’;
To display a person details whose first letter is ‘A’ and third letter is ‘d’, use the
command given below −
Select * from emp where ename LIKE ‘A_d_’;
String functions
are used to perform an operation on input string and return an output string.
Following are the string functions defined in SQL:
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
LOWER(): This function is used to convert the upper case string into lower case.
Syntax: SELECT LOWER('GEEKSFORGEEKS.ORG');
Output: geeksforgeeks.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’
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
EXP(X)
This function returns the value of e (the base of the natural logarithm) raised to the
power of X.
SQL>SELECT EXP(3);
FLOOR(X)
This function returns the largest integer value that is not greater than X.
SQL>SELECT FLOOR(7.55);
FLOOR(7.55)
7
MOD(N,M)
This function returns the remainder of N divided by M. Consider the following
example −
SQL>SELECT MOD(29,3);
PI()
This function simply returns the value of pi. SQL internally stores the full double-
precision value of pi.
SQL>SELECT PI();
POWER(X,Y)
These two functions return the value of X raised to the power of Y.
SQL> SELECT POWER(3,3); 27
SQRT(X)
This function returns the non-negative square root of X. Consider the following
example −
SQL Aggregate Functions
SQL aggregation function is used to perform the calculations on multiple
rows of a single column of a table.
It returns a single value.
It is also used to summarize the data.
•COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table.
•COUNT(*) considers duplicate and Null.
Ex.SELECT COUNT(*) FROM PRODUCT_MAST;
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on
numeric fields only.
Ex.SELECT SUM(COST) FROM PRODUCT_MAST;
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.
Ex. SELECT AVG(COST) FROM PRODUCT_MAST;
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.
Ex.SELECT MAX(RATE) FROM PRODUCT_MAST;
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.
Ex. SELECT MIN(RATE) FROM PRODUCT_MAST;
DATE & TIME function
1. Sysdate:
The SYSDATE() function returns the current date and time.
Ex. select sysdate from dual;
2.round(sysdate): The ROUND() function is used to get the date rounded to the
unit specified by the format model.
Syntax:
ROUND(date [, fmt ])
The Round(date) is an Oracle Date/Time function. This function returns the date
rounded by the given measure.
Ex. select ROUND(TO_DATE ('25-NOV-18'),'YEAR') from dual;
select ROUND(TO_DATE ('25-NOV-18'),‘MONTH') from dual;
last_day(sysdate): LAST_DAY returns the date of the last day of the month that
contains date. The return type is always DATE, regardless of the data type of the date
argument.
Syntax:
LAST_DAY( Date );
Ex.select last_day(sysdate)from dual;
SET operators are special type of operators which are used to combine the result
of two queries.
SET operators are:
UNION
UNION ALL
INTERSECT
MINUS