My SQL Notes
My SQL Notes
■ SELECT COMMAND:
The SELECT command is used to make queries on the database. A query is a command
that
is given to produce certain specified information from the database table(s). The SELECT
command can be used to retrieve a subset of rows or columns from one or more tables.
The syntax of Select Command is:
SELECT <Column-list>
FROM <table_name>
[Where <condition>]
[GROUP BY <column_list>]
[Having <condition>]
[ORDER BY <column_list [ASC|DESC ]>]
Example:
SELECT * FROM ADDRESS WHERE SNo=100;
■ SQL Constraint:
A Constraint is a condition or check applicable on a field or set of fields.
■ NOT NULL/UNIQUE/DEFAULT/CHECK/PRIMARY KEY/FOREIGN KEY
Constraint:
CREATE TABLE student (rollno integer NOT);
CREATE TABLE student (rollno integer NOT NULL, Sclass integer, Sname
varchar(30), Sclass DEFAULT 12 );
CREATE TABLE student (rollno integer CHECK (rollno>0), Sclass integer, Sname
varchar(30));
CREATE TABLE student (rollno integer NOT NULL PRIMARY KEY, Sclass integer,
Sname varchar(30));
Not Null and Default constraints can be applied only at column level rest all constraints
can be applied on both column level and table levels.
MySQL functions:
A function is a special type of predefined command set that performs some operation and
returns a single value.
Single-row functions return a single result row for every row of a queried table. They are
categorized into: Numeric functions, String functions, and Date and Time functions.
1) Numeric Functions
• POWER( ) : Returns the argument raised to the specified power. POW ( ) works
the same way.
Example: (i) POW(2,4); Result:16 (ii) POW(2,-2); Result:0.25 (iii) POW(-2,3) Result: -8
• ROUND( ) : ROUND(X) Rounds the argument to the zero decimal place, Where
as ROUND(X,d) Rounds the argument to d decimal places.
Example :(i) ROUND(-1.23); Result: -1 (ii) ROUND(-1.58); Result: -2
(i) ROUND(1.58); Result: 2 (iv)ROUND(3.798, 1);Result: 3.8
(v) ROUND(1.298, 0); Result: 1 (vi) ROUND(23.298, -1); Result:
20
3) Date/Time Functions
CURDATE( ) : Returns the current date
Example: CURDATE( ); Result:'2014-07-21'
NOW( ): Returns the current date and time
Example: NOW( ); Result: '2014-07-21 13:58:11'
SYSDATE( ) : Return the time at which the function executes
Example: SYSDATE( ); Result:'2014-07-21 13:59:23‟
7
■ The GROUP BY clause groups the rows in the result by columns that have
the same values. Grouping can be done by column name, or with aggregate
functions in which case the aggregate produces a value for each group.
■ The HAVING clause place conditions on groups in contrast to WHERE clause
that place conditions on individual rows. While WHERE condition cannot
include aggregate functions, HAVING conditions can do so.
■ ALTER TABLE COMMAND:-
The ALTER Table command is used to change the definition (structure) of
existing table. Usually , it can:
(i) Add columns to a table
(ii) Delete columns
(iii) Modify a column The syntax of this command is: For Add or
modify column:
ALTER TABLE <Table_name> ADD/MODIFY <Column_defnition>;
For Delete column
ALTER TABLE <Table_name> DROP COLUMN <Column_name>;
Example :
0 To add a new column address in EMP table command will
be : ALTER TABLE EMP ADD (address char (30));
0 To modify the size of sal column in EMP table, command will
be: ALTER TABLE EMP MODIFY (sal number(9,2) );
0 To delete column Address from Table EMP the command
will be: ALTER TABLE EMP DROP COLUMN address;
8
Q3. Consider the following tables HOSPITAL. Give outputs for SQL queries (i) to (iv) and
write SQL commands for the statements (v) to (viii).
No Name Age Department Dateofadmin Charge Sex
1 Arpit 62 Surgery 21/01/06 300 M
2 Zayana 18 ENT 12/12/05 250 F
3 Kareem 68 Orthopedic 19/02/06 450 M
4 Abhilash 26 Surgery 24/11/06 300 M
5 Dhanya 24 ENT 20/10/06 350 F
6 Siju 23 Cardiology 10/10/06 800 M
7 Ankita 16 ENT 13/04/06 100 F
8 Divya 20 Cardiology 10/11/06 500 F
9 Nidhin 25 Orthopedic 12/05/06 700 M
10 Hari 28 Surgery 19/03/06 450 M
9
Q4. Consider the following tables BOOKS. Write SQL commands for the statements (i)
to (iv) and give outputs for SQL queries (v) to (viii).
Table : BOOKS
10
(vi) Select dayofmonth(„2012-05-11‟);
(vii) Select month(„2010-02-07‟);
2. Consider the table STUDENT given below, write SQL Commands for (i) to (iv)
and output for (v) to (viii)
3. Write an SQL query to create the table books with following structure.
11
Table : SchoolBus
12
9 RUBINA 500 COMP. Sc. 62 I
10 VIKAS 400 MATHS 57 II
(a) List the names of those students who have obtained DIV 1 sorted by NAME.
(b) Display a report, listing NAME, STIPEND, SUBJECT and amount of stipend received
in a year assuming that the STIPEND is paid every month.
(c) To count the number of students who are either PHYSICS or COMPUTER SC graduates.
(d) To insert a new row in the GRADUATE table: 11,”KAJOL”, 300, “computer sc”, 75, 1
(e) Give the output of following sql statement based on table GRADUATE:
(i) Select MIN(AVERAGE) from GRADUATE where
SUBJECT=”PHYSICS”;
(ii) Select SUM(STIPEND) from GRADUATE WHERE div=2;
(iii) Select AVG(STIPEND) from GRADUATE where AVERAGE>=65;
(iv) Select COUNT(distinct SUBDJECT) from GRADUATE;
(f) Assume that there is one more table GUIDE in the database as shown below:
Table: GUIDE
MAINAREA ADVISOR
PHYSICS VINOD
COMPUTER SC ALOK
CHEMISTRY RAJAN
MATHEMATICS MAHESH
g) What will be the output of the following query:
SELECT NAME, ADVISOR FROM GRADUATE, GUIDE WHERE SUBJECT=
MAINAREA;
13