0% found this document useful (0 votes)
21 views5 pages

4 - More On Databases and SQL - 09xii

Uploaded by

h0762281
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

4 - More On Databases and SQL - 09xii

Uploaded by

h0762281
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

MORE ON DATABASES AND SQL 1

SQL GROUP FUNCTIONS

A function is a special type of predefined command set that performs some operation and returns a
single value. We are already familiar with this term by now.

There are two types of functions

a) Single row / Scalar functions


b) Multiple row / group / aggregate) functions
a) Single row/ Scalar functions – work with a single row at a time. A single row function returns a
result for every row of a queried table. We have already seen these functions in class XI like various
String functions(Len, lcase etc), mathematical functions, date functions etc.
eg. SELECT LENGTH(“abcd”);
returns 4 as the output.

b)Aggregate or Group functions:


MySQL provides Aggregate or Group functions which work on a number of values of a
column/expression and return a single value as the result. Some of the most frequently used.
Aggregate functions in MySQL are : MIN(), MAX(), AVG(), SUM(), COUNT().

Data Types in aggregate functions:


MIN(), MAX(), and COUNT() work on any type of values - Numeric, Date, or String. AVG(), and SUM()
work on only Numeric values (INT and DECIMAL).

S.NO FUNCTION PURPOSE


1. MAX ( ) Returns the MAXIMUM of the values under the specified column.

EX: To find the highest cost of any type of shoe in the factory.

SELECT MAX(cost) FROM shoes;

2. MIN ( ) Returns the MINIMUM of the values under the specified column.

EX: To find the lowest cost of any type of shoe in the factory.

SELECT MIN(cost) FROM shoes;

3. AVG ( ) Returns the AVERAGE of the values under the specified column.

EX: To find the average margin from shoes table.

SELECT AVG(margin) FROM shoes;

4. SUM( ) Returns the SUM of the values under the specified column.

EX: To find the total quantity present in the stock.

Amit Kumar Singhal


Page 1
MORE ON DATABASES AND SQL 2

SELECT SUM(Qty) FROM Shoes;

5. COUNT ( ) Returns the COUNT of the number of values under the specified
column/expression.

EX: To count the total number of records in the table Shoes.

SELECT COUNT(*) FROM shoes;

NULLs in aggregate functions: Aggregate functions totally ignore NULL values present in a column.
GROUP BY: GROUP BY clause is used in a SELECT statement in conjunction with aggregate functions
to group the result based on distinct values in a column.

SELECT type, SUM(qty) FROM shoes


GROUP BY type;
HAVING: HAVING clause is used in conjuction with GROUP BY clause in a SELECT statement to put
condition on groups.

SELECT type, SUM(qty) FROM shoes


GROUP BY type
HAVING SUM(qty) > 1500;

WHERE Vs HAVING: WHERE is used to put a condition on individual row of a table whereas HAVING
is used to put condition on individual group formed by GROUP BY clause in a SELECT statement.
JOINS
A join is a query that combines rows from two or more tables. Joins are foundation of multiple table query
processing in SQL.

Whenever data is to be brought from multiple tables a join is used. To make a query in which rows of two tables
are joined, we must specify the join columns that contain corresponding information in the two tables. There
has to be a column field in the tables that are being joined. A SELECT with WHERE clause is used for this.

Cartesian Product (or Cross Join): Cartesian product of two tables is a table obtained by pairing
each row of one table with each row of the other. A cartesian product of two tables contains all the
columns of both the tables.

Amit Kumar Singhal


Page 2
MORE ON DATABASES AND SQL 3

The number of columns in the Cartesian product is the sum of the number of columns in both the
tables and rows will multiple in both the tables . In SQL, Cartesian product of two rows is obtained by
giving the names of both tables in FROM clause. An example of Cartesian product is shown below:

SELECT * FROM order_table, product;

To give the output of this query, MySQL will pair the rows of the mentioned tables as follows:

Equi-Join: An equi join of two tables is obtained by putting an equality condition on the Cartesian
product of two tables. This equality condition is put on the common column of the tables. This
common column is generally primary key of one table and foreign key of the other.

Foreign Key: It is a column of a table which is the primary key of another table in the same database.
It is used to enforce referential integrity of the data.

Amit Kumar Singhal


Page 3
MORE ON DATABASES AND SQL 4

Referential Integrity: The property of a relational database which ensures that no entry in a foreign
key column of a table can be made unless it matches a primary key value in the corresponding column
of the related table.
Union: Union is an operation of combining the output of two SELECT statements. Union of two
SELECT statements can be performed only if their outputs contain same number of columns and data
types of corresponding columns are also the same. The syntax of UNION in its simplest form is:

Constraints: These are the rules which are applied on the columns of tables to ensure data integrity
and consistency. Or a constraint is a condition or check applicable on a field or set of fields.
Syntax:- Create table < table name>

( <column name> <data type> <size> <column constraint>,

<column name> <data type> <size> <column constraint>,

<column name> <data type> <size> <column constraint> ….. );

1. Default constraint:- A default value can be specified for a column using default clause . When a user does
not enter a value for the column , automatically the defined default value is inserted in the field.

2. Check Constraint:- This constraints limits values that can be inserted into a column of a table.

3. Unique Constraint:- This constraint ensures that no two rows have the same value in the specified
column(s).

4. Primary key :- This constraint declares a column as the primary key of the table. This constraint is similar to
unique constraint except that only one column can be applied in this constraint. The primary key cannot allow
NULL values.

5. NOT NULL:- This constraint ensures that the column cannot be left empty.
Exam:- Create table emp
( empno int(3) primary key,
empname vharchar(20) unique key
city varchar(20) default =’ delhi’,
age int (3) check age>30,
salary float(7,2) NOT NULL);

Amit Kumar Singhal


Page 4
MORE ON DATABASES AND SQL 5

ALTER TABLE: ALTER TABLE command can be used to Add, Remove, and Modify columns of a table.
It can also be used to Add and Remove constraints.

Syntax: Alter table <tablename>


{ADD/DROP/MODIFY}(colname datatype(size));

To add a new column city :


Alter table student
Add( city varchar(20));

To modify width /size of city column :

Alter table student


Modify( city varchar(25));

To remove city column :


Alter table student
Drop ( city);

DROP TABLE: DROP TABLE command is used to delete tables.

Syntax: Drop {table/ database} <tablename/ database name>;

Ex:- Drop table emp;

Drop database abc;

Amit Kumar Singhal


Page 5

You might also like