Xii Ip SQL Notes
Xii Ip SQL Notes
Relational model
Relation- A relation or table is two-dimensional structure arranged in rows and columns.
Domain- it is collection of possible values from which the value for a column is derived.
TYPES OF KEYES:
Primary key- a set of one or more attributes which is used to identify a record in the relation at first preference is called
primary key.
Candidate key- a table may have multiple keys which can identify a record in a table. All such columns are called
candidate key.
Alternate key- a candidate key that is not a primary key is callled alternate key.
Foreign key-a foreign key is non-key column in a table whose value is derived from the primary key of some other table.
MY SQL -INTRODUCTION
My SQL is an open source, free and powerful relational database management system software based on sql.
The main features of MySQL are-
1. open source &free of cost
2. portability
3. security
4. connectivity
5. Query language
DDL-Data definition language – it’s allowed you to perform task related to data definition.
ex- Create, alter, drop
Types of DDL commands:
1) CREATE: Use to create a table.
5) Using column alias, the column will have a different name for output purpose.
Example: Select Name, Marks as Results from Student;
It will display Marks column as a temporary name “result”
7) Condition based on a list: To specifies list of values we can use IN and NOT IN operator.
Example: a) Select * From Student Where subject IN (“CS”, “IP”, “PE”);
It will display details of CS, IP and PE students.
b) Select name, class From Student Where subject NOT IN (“CS”, “IP”, “PE”);
It will display details of all the subject except CS, IP and PE. 8) Condition based on patterns:
a) To list the students whose name starts with “A”. Select * from Student Where name LIKE “a%”;
b) To list the students whose name end with “A”.
Select * from Student Where name LIKE “%a”;
c) To list the students whose name, have four characters in their name.
Select * from Student Where name LIKE “_ _ _ _”; Four underscore to represents four characters.
9) Sorting records: ORDER BY clause is used to sort the records in ascending or descending. By default, ORDER By
arrange the records in ascending order.
Example: Select * from Student Order By marks;
It will display the details of students in ascending order of their marks. We can also use the keyword ASC in ascending
order.
For descending order:
Select * from Student Order By marks DESC;
The keyword DESC will display the records in descending order. We can also apply order by on two different columns in
same query.
Select * from Student Order By marks DESC, name ASC;
It will display Marks in descending order and Name in ascending order
Handling NULL values:
NULL represents empty values or no values in SQL.
1) To insert NULL values in a table. We can type NULL without quote to insert NULL value in a specific column and NULL
will be inserted in that column.
INSERT INTO Student VALUES (102,” Riya”, 12, NULL); 2) To display NULL values We use IS NULL keyword to display NULL
values.
Select * from Student Where Subject IS NULL;
It will display details which contain NULL in column subject.
2) To display non-NULL values. We use IS NOT NULL to display non-NULL values.
Select * from Student Where Subject IS NOT NULL;
TCL-transaction control language – it controls transition process.
ex- commit, rollback, save point.
1) Numeric data types: Used for representing number in a column e.g. – int(m) – Integer of maximum length m i.e.
maximum number of digits allowed in m. float (m, d), decimal (m, d), numeric (m, d) – Real number of maximum
length m where maximum number of digits permissible after decimal point is d and before decimal point is m-d.
2) Date & Time data types: Used to represent date, time, or both in a column. Data is enclosed with quotes ‘’ or ““.
e.g. - date, date & time, time.
3) String / Text data types: Used to represent text in a column. Data is enclosed with quotes ‘’ or ““. e.g. – char(m)
– Fixed length character of length m where 1 character takes 1 Byte in memory. Memory space is wasted if text
size is less than m. varchar(m) – Variable length character allowing maximum number of characters m. It saves
memory allocation for text having lesser size than m.
4) blob – Binary Large object for huge size text.
5) NULL – NULL is said to be the absence of any value in a column. No arithmetic or comparison operation
FUNCTION
A function is a special type of command in MySQL that performs some operation on table returns a single value as result.
Single row function- these functions which are applied on a single value and return a single value are called single row
functions.
There are three categories of single row function in mysql
1. Math or numeric function
2. Text or string function
3. Date &Time function
Math or numeric function- these functions may accept values and performing required operation, returns numeric
values as result.
POWER- it is used to compute power of given value as parameter.
Syntax: select power (value)
ex: select power (3,3) = 27
ROUND- it is used to display the number to nearest number with rounding up if the next digit is more than 5.
syntax: select round(value)
ex: select round (35.99) = 36
MOD: this function returns the remainder after division of the number with divisor.
Syntax: select mod(value)
ex: select mod (53.3) = 2
SQRT – this function returns square root of given number.
Syntax: select sqrt(value)
ex: select sqrt (100) = 10
ABS: this function returns the number into positive number as an absolute value.
Syntax: select abs(value)
ex: select abs (-15) = 15 m
Truncate: this function returns a number after removing specified digits as parameter.
Ex: select truncate (36.566666) = 36.56
Sign: it will return 1 if the number is positive and returns -1 if the number is negative.
ex- select sign (19) = 1
TEXT OR STRING FUNCTION
String-can perform various operations on alphanumeric data.
ucase/upper case: this function is used to convert the text into upper case into capital.
lcase/lower case- this function is used to convert the enclosed text into lower case small letters.
Mid-this function returns the text starting from a specified number of letters to a specified letter from the enclosed text
or column value.
Length-this function returns the number of letters from the text including white space.
Left- this function is used to return specified left side letters from the enclosed text or column values.
Right-it is exactly reverse than left, display the result from right side of selected text.
Instr ()- it will check the specified text from the enclosed text and return a number from where the specified text is
starting.
DATE FUNCTION
Date and time function: these functions perform operations on date and time data.
Current date ()-this function is used to display date values in ‘YYYYMMDD’ format.
Date ()- it will display the date from the selected dates. The format will be ‘yyyy-mm-dd’ or ‘yyyy-mm-dd’.
Months ()- it will display the month number from the specified date.
Month name ()- this function will return the name of month from the specified date.
Year ()- this function returns tear from the specified date.
Day ()- this function will display from the specified date. supposed the date is ‘2020-08-17’ then the output will be 17.
Day name ()- this function returns the name of the day of the week of specified date.
Day of month ()- this function returns the day number from the specified date.
Day of week ()- it returns the day of the week from the specified date. It starts with sunday=1.
Day of year – it returns the day number from the year.
Aggregate functions
aggregate functions are also called multiple rows functions these functions work on a set of records as a whole and
return a single value for each column of the records on which the function is applied.
We will cover the following aggregate functions in database query using sql as per our syllabus.
1. Avg
2. count
3. max
4. min
5. sum
Avg-it computes average for given values as parameters.
ex- select avg(marks) from student;
Count- this function counts the total no. Of values from the given set of rows.
Ex-select count(marks)from student;
Max- this function is used to return maximum value from the given set of values.
ex- select max(marks) from student;
Min- it will return the minimum value from given set of values.
ex- select min(marks)from student;
Sum- it will return the addition of specified values. this function can work with column as well with specific where
condition.
Ex-select count(marks)from student;
Difference between single row function & aggreagate function: -
Single row function
1. It operates single row at a time.
2. It returns one result per row.
3. It can be used in select, where and order by clause.
4. Ex: math, string, date function etc.
Aggregate function
1. it operates on multiple rows.
2. It returns one result for multiple rows.
3. It can be used clause only.
4. Ex: max (), min(),Avg() etc.
GROUP BY CLAUSE- the group by clause can be used in a select statement to collect data across multiple records and
group the results by one or more groups.
HAVING CLAUSE- the having clause is used in combination with the group .it can be used in a select statement to filter
the records by specifying a condition which a group by returns:
THE ORDER BY CLAUSE-the sql order by clause is used to sort data in ascending order bases on one or more columns.the
order by keyword is used to sort the result-set by one or more fields in a table.
difference between where clause and having clause-
where clause-
1. To use filters individual rows before grouping.
2. It filters rows that don’t meet certain criteria.
3. It works applies conditions to row data.
4. Use with select update, and delete statements.
Having clause-
1. To use filters groups after grouping.
2. It filters groups with certain count, sum or average values.
3. It works applies conditions to the results of aggregate functions.
4. Use with select statement and group by clause.
Difference between order by and group by-
Order by-
1. Sorts the rows of a result set based on one more column.
2. Arrange the data in ascending or descending order.
3. Doesn’t change the structure of the data, just the order presentation.
4. Ex- select *from student order by marks desc;
Group by-
1. Groups rows that have the same values in one or more columns.
2. Typically used with aggregate function.
3. Reduces the number of rows in the result set creating a summary for each group.
4. Ex- select*department, count (*) from employees’ group by department;
Difference between single row function and aggregate function-
Single row function-
1. It operates on single row at a time.
2. It returns one result per row.
3. It can be used in select, where and order by clause.
4. Ex- math, string, date function.
Aggregate function-
1. It operates on multiple rows.
2. It returns one result for multiple rows.
3. It can be used in the select clause only.
4. Ex- max (), min (), avg (), sum () etc.