3 CZX Be Wgwyd 05 B RFDWL G
3 CZX Be Wgwyd 05 B RFDWL G
Functions
A function is a predefined command set that performs some operation and returns the single
value.
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
(iii) 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
(vii) ROUND( 25.298,-1); result: 30
TRUNCATE() : Truncates the argument to specified number of decimal places.
Example:
(i) TRUNCATE (7.29,1) Result: 7.2
(ii) TRUNCATE(27.29,-1) Result: 20
SIGN() : Returns sign of a given number.
Example :
(i) SIGN (15) Result : 1 :
(ii) SIGN (-15) Result : -1 :
(iii) SIGN (0) Result : 0.
SQRT : Returns the square root of given number.
Example :
(i) SQRT (25) Result : 5
What is SQL?
Ans. SQL is Non-procedural universal data access language used to access and manipulate data
stored in nearly all the data bases available currently. SQL standards are defined by ANSI
(American National Standards Institute). SQL statements are used to retrieve and update data in a
database. SQL works with database programs like MySQL, MS Access, DB2, Informix, MS SQL
Server, Oracle, Sybase, etc.
Differentiate between DDL and DML?
Ans. Data Definition Language (DDL): This is a category of SQL commands. All the commands
which are used to create, destroy, or restructure databases and tables come under this category.
Examples of DDL commands are - CREATE, DROP, ALTER. Data Manipulation Language (DML): This
is a category of SQL commands. All the commands which are used to manipulate data within
tables come under this category. Examples of DML commands are - INSERT, UPDATE, DELETE.
What is a constraint?
Ans. : A constraints is a condition or check application on a field or set of fields. Example: NOT
NULL (ensure that column con not have null value), CHECK (make sure that all value satisfy
certain criteria), UNIQUE (ensure that all values in a column are different) etc.
What are single row functions ?
Ans. : Single Row Function work with a single row at a time. A single row function returns a result
for every row of a quired table Examples of Single row functions are Sqrt(), Concat(), Lcase(),
Upper(), Day(), etc. Compare CHAR and VARCHAR data types.
Ans. The CHAR data-type stores fixed length strings such that strings having length smaller than
the field size are padded on the right with spaces before being stored. The VARCHAR on the other
hand supports variable length strings and therefore stores strings smaller than the field size
without modification.
What are the differences between DELETE and DROP commands of SQL?
Ans. : DELETE is DML command while DROP is a DDL command. Delete is used to delete rows
from a table while DROP is used to remove the entire table from the database.
What do you understand by MySQL Client?
Ans. : MySQL Clients are programs that connect to MySQL Server and issue queries in predefined
format. Explain with the help of an example that why should a transaction be executed as a whole
or it should be not executed at all.
Ans. : Suppose Raunak's account number is 3246 and his aunt's account number is 5135. In order
to process the cheque presented by Raunak, the following two SQL commands need to be
executed on the database maintained by the bank:
UPDATE Savings SET balance = balance - 2000
WHERE account_no = 5135;
UPDATE Savings SET balance = balance + 2000
WHERE account_no = 3246;
Query Based question & answers
1. The Pincode column of table 'Post' is given below-
Ans. : SELECT Pincode from Post where Pincode LIKE " %1" ;
i. SELECT Pincode from Post where Pincode LIKE " 0%" ; i) 110001 ii) No Output
2. A table "Animals" in a database has 3 columns and 10 records. What is the degree and rdinality
of this table?
Ans. : Degree 3 and Cardinality=10 3. Answer the question based on the table VOTER given
below:
(i) Write the command to delete all the rows of particular voter from the table voter where voter ID
between 10 and 20.
Ans. : Delete from VOTER where V_id between 10 and 20; (ii) Delete the table physically.
4. Write MySql command to create a furniture table including all constraint.
Ans. : Drop table VOTER;
Lab Activity 2: Open school database, then select student table and use following SQL
statements.
TYPE THE STATEMENT, PRESS ENTER AND NOTE THE OUTPUT
1.To display all the records form STUDENT table.
SELECT * FROM student ;
2. To display ony name and date of birth from the table STUDENT.
SELECT StdName, DOB FROM student ;\
3. To display all students record where percentage is greater of equal to 80 FROM student table.
SELECT * FROM student WHERE percentage >= 80;
4. To display student name, stream and percentage where percentage of student is more than
80SELECT StdName, Stream, Percentage WHERE percentage > 80;
5. To display all records of science students whose percentage is more than 75 form student table.
SELECT * FORM student WHERE stream = ‘Science’ AND percentage > 75;
Lab Activity 3: Open school database, then select student table and use following SQL
statements
TYPE THE STATEMENT, PRESS ENTER AND NOTE THE OUTPUT
1. To display the STUDENT table structure.
DESCRIBE Student;
2. To add a column (FIELD) in the STUDENT table, for example TeacherID as VARCHAR(20);
ALTER TABLE Student ADD TeacherID VARCHAR(20);
3. Type the statement
DESC Student;
Press enter key, now note the difference in table structure.
4. Type the statement and press enter key, note the new field that you have added as TeacherID
SELECT * FROM student;
5. To modify the TeacherID data type form character to integer.
ALTER TABLE Student MODIFY TeacherID INTEGER ;
DESC Student;
SELECT * FROM student;
Lab Activity 4
1. To Drop (Delete) a field form a table. For e.g you want to delete TeacherID field.
ALTER TABLE Student DROP Teacher ID;
2. To subtract 5 form all students percentage and display name and percentage.
SELECT name, percentage - 5 FROM Student;
3. Using column alise for example we want to display StdName as Student Name and DOB as Date
of Birth then the statement will be.
SELECT StdName AS "Student Name",
DOB As “Date of Birth” FROM Student;
4. Display the name of all students whose stream is not Science
SELECT StdName FROM student
WHERE Stream <> ‘Science’;
5. Display all name and percentage where percentage is between 60 and 80
SELECT StdName, percentage FROM student WHERE percentage >=60 AND
percentage<=80 ;
Lab Activity 5:
1. To change a student name from SWATI MISHRA to SWATI VERMA whose StdID is 1014 andalso
change percentage 86.
UPDATE Student SET StdName = ‘SWATI VERMA’, percentage = 86 WHERE StdId = 1014;
2. To delete the records form student table where StdId is 1016.
DELETE FROM Student WHERE StdID = 1016;
3. Type the following SQL statement and note the output.
SELECT * FROM Student WHERE StdName LIKE 'G_' ;
SELECT * FROM Student WHERE StdName='G';
SELECT * FROM Student WHERE StdName LIKE 'G%' ;
SELECT * WHERE Student WHERE StdName='%G%' ;
4. Display all the streams in student table.