Scalar Function in SQL
Introduction to SQL
SQL stands for Structured Query Language.
SQL is a standard language for accessing and manipulating databases and records
It helps to Manage Data in database according to Our Need.
Let see what we can do with SQL
1.Create a Database .
2.Create table inside database to store data.
3.Insert data using SQL Query.
4.Update data using SQL Query
5.Delete data using SQL Query
6.Retrive Record from Datable as per our need.
7.Set permission to Database and Table.
8.We can create Store Procedure to fetch record or manipulate record.
We have to use Some Command to Manipulate Database record.
Insert,Update,Delete,Select...........etc.
There are Some Syntax in SQL which is used to Manipulate record in database
which is ANSI standard Syntax and Command
.Like To Fetch all records From Table we will write
select * from TableName
To Delete Record From Table
Delete TableName where ID=X
This will delete record from table where ID is X
Order by In SQL
Order by in Sql is used to sort record in ascending or desending order
We will write query like
SELECT X, Y, ...
FROM mytable
ORDER BY x, y,Z ... ASC|DESC;
by default it gives record in ascending order but get record in ascending order we will use order by Desc
Example
| StudentID | StudentName | Age | Marks |
|---|---|---|---|
| 1 | Navneet | 11 | 82 |
| 2 | Nitesh | 16 | 90 |
Select * from Studenttable order by age desc
Result as below
| StudentID | StudentName | Age | Marks |
|---|---|---|---|
| 2 | Nitesh | 16 | 90 |
| 1 | Navneet | 11 | 82 |
Case In Sql
While wring a query or fetching record from sql database we need some condition to be satisfied according to record we fetch. where we use Case as Condition
It returns value when and then conditions(when a condition satisfied then it returns value else it moves to check for next condition)
Lets Check how to write Case Statement
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
When it satisfy first condition it stops else move for next one and gives record accordingly.
| StudentID | StudentName | Age | Marks |
|---|---|---|---|
| 1 | Navneet | 11 | 82 |
| 2 | Nitesh | 16 | 90 |
CASE
WHEN Marks> 85 THEN 'Top'
WHEN Quantity <30 THEN 'Fail'
ELSE 'Good'
END AS Position
FROM Student;
Joins In SQL
Join In SQL
A join is a clause in sql used to join two or more than two table rows based on relationship between themTypes of Joins1.Inner Join
This join gives record having matching values in tablesQuery will be written asWe have two table class and StudentsSelect Class.ClassName,Student.Student Namefrom Studentsinner join Class on Class.StudentID=Student.StudentID
2. Left Join
This Join give all data from left table and matching data from right tableQuery will be written as* means all recordSelect *from Studentsleft join Class on Class.StudentID=Student.StudentID3.Right Join
This Join give all data from right table and matching data from left tableQuery will be written asSelect *from Studentsright join Class on Class.StudentID=Student.StudentID
4.Full Outer Join
This join return all record from both table having matched dataQuery will be written asSelect *



