SQL
SQL
--Q.What is data?
--Collection of meaniful information.
--OR
--Collection record information.
--Q.what is table ?
--it is collection of rows and columns.
--In SQL
--Blue color indicates system defined keywords
--for ex
create, use, insert etc
--Pink color indicates system defined functions
--for ex
sum,min,max etc
--Data types
--Type of data/value of an object can hold is known as data type.
--A].Numeric data type
--1.BIT
--it stores value 0 or 1
--2.TINYINT
--It will store the value ranging from 0 to 255
--3.SMALLINT
--it will store value ranging -32768 to 32767
--4.Decimal
--an exact fixed point number
--5. INT
--it stores an integer value i.e. ranging from -2147483648 to 2147483647
--3.nchar
--It is static memory allocation and it can store 4000 charecters (1 char it will
occupy 2bytes)
declare @value nchar(4000) = 'AMAR'
print @value
print datalength(@value)
print len(@value)
--4.nvarchar
--It is dynamic memory allocation and it can store 4000 charecters (1 char it will
occupy 2bytes).
--3.Datetime
--It will allow you to insert date and time together.
--YYYY/MM/DD HH:MM AM/PM
-----------------------------------------------------------
Class 2
--Create
--This Is DDL SQL statement and used to create database and Table.
--In SQL if you want to terminate SQL statement then use ; at the end of statement.
--SELECT
--Select statement is used to select the data which you have written
--This is DML statement and used to fetch the records from table.
select 88888
--METHOD-II
--We dont have restrictions to insert the data as per column sequence defined in
table.
--While inserting the data we have to mention column names in insert statement.
insert into FirstTable (FID,FirstName) values (2,'Amit')
---------------------------------------------------------------
Class 3
--1.Where
--Where clause is used with comaprision operator,arithmatic and logical operators.
--It used to filter the specific condition or we can select a particlar records
from table.
--Operators
--1.Comaprision Operator
--2.Logical Operator
--3.Arithmatic operator
--4.IN and NOT IN
--5.Between and NOT BETWEEN
--6.LIKE
--1.Comaprision Operator
--it is used to compare the condition provided in filter clauses or where clause.
-- = - equal to
-- > - greater than
-- < - less than
-- >= - greater than equal to
-- <= - less than equal to
-- != or <> -not equal to
--AND oeartion
--A B O/P
--0 0 0
--0 1 0
--1 0 0
--1 1 1
--A B O/P
--False False False
--False True False
--True False False
--True True True
--OR
--It will work like addition
--OR operation
--A B O/P
--0 0 0
--0 1 1
--1 0 1
--1 1 1
--A B O/P
--False False False
--False True True
--True False True
--True True True
select * from employee where EID =5 or salary > 40000 or loc ='Pune'
select * from employee where eid => 1,2 -- incoreect syntax because comparision
opeartor will allow you to insert only one condition.
select * from employee where Loc between 'A' and 'N' --A to N-1
--Not Between
select * from employee where EID not between 2 and 6
select * from employee where Loc not between 'A' and 'N' --
--5.LIKE
--LIKE operator is used to search for a specified pattern in a column.
--Mostly like operator is used in where clause.
--Like operator used wildcards for searching a pattern
--1. % - Represents zero,one or multiple charecters or numbers./ A substitue for
Zero or more characters
--2. _ - Represents one or single charecters./A A substitue for exactly one
character.
--3.[Charlist] - Any single charecter in charlist ex: [ABC]
--4.[^Charlist] -any charecter not in charlist
--ex: Seeta,meeta,geeta sena, sona siya
--'S%' - start with 'S' chareter and it will display all the names which starts
with S.
--'%S' - End with 'S' charetcer and it will display all the names which END with S.
--'%S%' -Anywhere inside record/column if 'S' charetcer and it will display all the
names which starts or ends or anywhere inside into a column.
select * from employee where FirstName like 's%' ---at the start of name s
select * from employee where FirstName like '%A' -- at the end of name a
select * from employee where FirstName like '%A%' --anywhere inside or start or
end.
select * from employee where FirstName like '[ARV]%' -- it will display the names
which start with A,R and V.
select * from employee where FirstName like '[^ARV]%' -- it will display the name
which not start with A,R and V.
select * from employee where firstname like '[A-E]%' -- it will diplay all the
names which is in range of A to E
--Q.How will you display the names which ends with r and t?
--Q.How will you display the name whose second last letter is T?
-----------------------------------------------------------
Class_4
--Arithmatic operator
--these operators used to perform mathematical operation like +,-,*,/ and %
-----------------------------------
--NULL Values
--A column with a NULL value is column with NO value
--NULL value is diffrent from 0(zero) and blank/empty sapce.
select * from employee where salary = NULL -- Blank /not possible to check by
using comaprision/logical/arithmatic opeartor
select * from employee where salary is NULL
----------------------------------------
order by
--This clause is used to sort the result in ascending (ASC) or Descending(DESC)
order.
--If the column contains NULL value in it and if we are performing order by
operation then NULL value should be first in ASC and Last in DESC.
----------------------------------------------------------
--synatx:
--UPDATE TABLE_NAME SET COLUMN_NAME ='VALUE' where COLUMN_NAME ='CONDITION'
--DELETE
--Delete statement is used to delete the data from table row by row.
--By using DELETE statement it is not possible to delete the structure.
--We can delete the table data at one time or row by row by specifying an
condition.
--syntax:
--DELETE TABLE_NAME where COULMN_NAME ='CONDITION'
--DROP
--DROP statement will delete the table structure as well as table data.
--Drop statement we can drop or delete the database.
--syntax:
--DROP TABLE TABLE_NAME
--DROP DATABASE DATABASE_NAME
DROP table UPDATE_DELETE -- it will delete table data as well as table structutre.
--Truncate
--Truncate statement allow you to delete the records from a table at once.
--It wont delete the structure of the table
--In Truncate you can't delete the data Row-By-Row by specifying a condtion.
-----------------------------------------------------------
--Functions
--1.min()
--2.max()
--3.count()
--4.TOP
--5.sum()
--6.avg()
--7.Distinct()
--1.MIN()
--This function will return the minimum value from a selected column
--2.MAX()
--This function will return maximum value from selected column
select * from employee order by salary Desc
--This function is used to count the number of records from table or column.
--Count function always accepts one argument.
--It wont count NULL values from the table or column.
--4.TOP()
--This function is used to display the top records from table as per specified
count.
--This function is very useful when we have large amount of data in table .
select Top 3 * from employee -- it will display the top 3 records from table.
--5.Sum
--this function add all recorfds from a column.
--it will return the total sum value in numeric expression.
--It will ignore NULL values from column.
select * from employee
select sum(salary) as totalsary from employee
select sum(loc) as totalsary from employee --exception : Operand data type varchar
is invalid for sum operator.
--6.avg()
--This function is used to find the avg of the column.
--It will ignore the NULL values.
--7.Distinct
--This function is used to find the unique records from column.
select * from employee
select distinct(Dept) from employee
---------------------------------------------------------------
--Group by
--Group by statements are used in conjection witrh aggregate functions to group
result-set by one or more columns.
--MIN,MAX,Count,AVG,SUM,
--Syntax:
--select column_name , aggregate_Function(column_name) from Table_NAME
--where Column_name <Condition>
--group by Column_name
--NOTE:
--An aggregate may not appear in the WHERE clause unless it is in a subquery
contained in a HAVING clause or a select list,
--and the column being aggregated is an outer reference.
--Q.How will you display the second highest salary department wise?
--Q.Display the department name with highest salary?
--Q.In group where we can use 'WHERE' clause and Why?