SQL Programming
SQL Programming
Introduction to SQL
• SQL stands for structured query language.
• Attribute
• Tuple
• Degree
• Cardinality:
• Schema
SQL data types
• SQL data types define the type of data which can be stored in the columns of a
table.
Data types mainly classified into three categories for every database.
- String Data types
- Numeric data Types
- Data and time Data types
8. LAST(): last is used to return the last value of the column mentioned.
Example:
Select last(customer_name) from customer;
SQL commands
9. IN operator: used to return the rows which meets the values
mentioned in IN operator. Basically as a replacement of multiple OR
statements:
Example:
Select * from student where student_course IN (AI, ML, DS);
12. Not NULL : used to select rows with non null values.
Example:
Select student_name, student_ID from student where marks is NOT
NULL;
SQL commands
13. Where clause: used to retrieve only those rows of the table which
satisfy some conditions.
Example:
Select empname, empID from employee where dept = ‘dev’;
14. AND condition: SQL AND condition is used in SQL query to create
two or more conditions to be met.
Example:
Select * from employee where dept = ‘HR’ AND location = ‘HYD’;
SQL commands
15. OR condition: used to get the result if they satisfy either of the
conditions mentioned.
Example:
SELECT *FROM emp WHERE Department = "IT" OR Location = "
Chennai";
16. AS : SQL 'AS' is used to assign a new name temporarily to a table
column or even a table.
Select count(DISTINCT employee_dept) AS dept from employee;
SQL commands
17. ORDER BY : Whenever we want to sort the records based on the columns
stored in the tables of the SQL database, then we consider using the ORDER
BY clause in SQL.
Example:
Select * from employee order by salary;
23. DELETE TABLE: The DELETE statement is used to delete rows from a
table. If you want to remove a specific row from a table you should use
WHERE condition.
Example:
Delete from customer where location = ‘Chennai’;
SQL commands
24.Rename table: used to change the name of the table.
Example:
RENAME employees to Dev_employees;
25. Truncate table: A truncate SQL statement is used to remove all rows
(complete data) from a table. It is similar to the DELETE statement with
no WHERE clause.
Example:
Truncate Table employee;
SQL commands
26. Select Into: it is used to copy the contents of one table into another.
Example:
SELECT * INTO Coding_Employees FROM Employee;
28. UPDATE Table: SQL UPDATE statement is used to change the data of the
records held by tables. Which rows is to be update, it is decided by a condition
Example:
UPDATE employee
SET salary = salary + 0.1* salary
Where rating = 2;
SQL operators
1. SQL Arithmetic Operators: performs the operation on the
numerical data of the database table
- Addition operation:
- Subtraction operation
- Multiplication operation.
- Division operation
- Modulus operation
Example: select empSalary + bonus from employee;
SQL operators
2. SQL Comparison operations: The Comparison Operators in SQL
compare two different data of SQL table and check whether they are the
same, greater, and lesser.
- Equal ( =)
- not equal( !=)
- Greater than (>)
- Less than (<)
- Greater than or equal to ( >=) - less than or equal to (<=)
Example: select * from employee where salary > 200000;
SQL operators
3. Logical operators: Logical Operators in SQL perform the Boolean
operations, which give two results True and False
- AND
- OR
- NOT
- IN
- LIKE
- ANY
- ALL
SQL operators
4. SET operators
-Union
-Union ALL
- Intersect
- minus
Aggregate functions in SQL
• count()
• SUM()
• AVG()
• MIN()
• MAX()
JOINS
• SQL Join statement is used to combine data or rows from two or more
tables based on a common field between them. Different types of Joins
are as follows:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
JOINS
Inner join: Inner join is used to get the common data from both the
tables which satisfies the condition.
Consider the following tables:
JOINS
Select student.name, student.age, course. Course_ID from student
inner join course on student. Roll_no = course.rollno ;
JOINS
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.
Select student.name, student.age, course. Course_ID from student left
join course on student. Roll_no = course.rollno ;
JOINS
Right 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.
Select student.name, student.age, course. Course_ID from student right
join course on student. Roll_no = course.rollno ;
JOINS
FULL JOIN: Full join combines the results of left join and
right join.
Select student.name, student.age, course. Course_ID from
student full join course on student. Roll_no =
course.rollno ;
Nested Queries
In nested queries, a query is written inside a query.
CREATE VIEW Brazil_Customers AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = 'Brazil';
PL/SQL Basics
• PL/SQL stands for "Procedural Language extension of SQL" that is
used in Oracle.
• PL/SQL is a block structured language
• PL/SQL includes procedural language elements like conditions and
loops.
• It allows declaration of constants and variables, procedures and
functions, types and variable of those types and triggers.
• It can support Array and handle exceptions
Variable in PL/SQL
PL/SQL allocates memory for the variable's value and the storage
location is identified by the variable name.
Parameters in a procedure:
• IN parameter
• OUT parameter
• INOUT parameter
PL/SQL for writing procedures
Syntax for creating procedure:
1.CREATE [OR REPLACE] PROCEDURE procedure_name
2. [ (parameter [,parameter]) ]
3.IS
4. [declaration_section]
5.BEGIN
6. executable_section
7.[EXCEPTION
8. exception_section]
9.END [procedure_name];
PL/SQL for writing procedures
Example:
1.create or replace procedure "INSERTUSER"
2.(id IN NUMBER,
3.name IN VARCHAR2)
4.is
5.begin
6.insert into user values(id,name);
7.end;
8./
Calling the procedure:
9.BEGIN
10. insertuser(101,'Rahul');
11. dbms_output.put_line('record inserted successfully');
12.END;
13./
PL/SQL for writing procedures
• DROP procedure: