0% found this document useful (0 votes)
2 views

Kabirr

Uploaded by

rao821066
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)
2 views

Kabirr

Uploaded by

rao821066
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/ 22

EMP & Dept Table

mysql> use Kabir;

Database changed

mysql> Creating the EMP_Kabir table

mysql> CREATE TABLE EMP_Kabir(

-> EMPNO INT PRIMARY KEY,

NAME VARCHAR(50),

SALARY INT,

DEPTID INT

-> FOREIGN KEY (DEPTID) REFERENCES Dept(DEPTlD));

Query OK, O rows affected (0.10 sec)

mysql> Inserting data into the EMP_Kabir table

mysql> INSERT INTO EMP_Kabir (EMPNO, NAME, SALARY, DEPTID) VALUES->

(10, 'George', 111400, 10),

> (105, 'Mary', 51600, 20),

(152, 'Sam', 110100, 10),


(215, 'Sarah', 43000, 30),

(244, 'Manila', 15600, 40),


(300, 'Robert', 68200, 30),

(335, 'Henry', 43000, 40), (400, 'Rachel', 115600, 10), (441, 'Peter', 58200, 20);

Query OK, 9 rows affected (0.02 sec)

mysql> Creating the Dept_Kabir table

mysql> CREATE TABLE Dept_Kabir (

DEPTID INT PRIMARY KEY,

DEPARTMENT VARCHAR(50),

CITY VARCHAR(50)

Query OK, O rows affected (0.04 sec)

mysql> Inserting data into the Dept_Kabir table mysql> INSERT INTO Dept_Kabir (DEPTID,
DEPARTMENT, CITY) VALUES (10, 'MARKETING', 'NEW DELHI'), (20, 'FINANCE',
'GURUGRAM'), (30, 'SALES', 'NOIDA'),

(40, 'MANUFACTURING', 'SONIPAT');

Query OK, 4 rows affected (0.01 sec)

QI. To display name, salary, city of all employees


mysql> SELECT EMP_Kabir NAME, EMP_Kabir.SALARY, Dept_Kabir.ClTY FROM EMP, dept

Where EMP.DEPTID = Dept.DEPTlD;

Q2. Name of columns which act as primary key and foreign key in tables Emp and Dept

Primary Key: In EMP_Kabir table: EMPNO In Dept_Kabir table: DEPTID

Foreign Key: In EMP_Kabir table: DEPTID (referencing DEPTID in Dept table)

Q3. To display the number of records in each DEPTID.

mysql> SELECT DEPTID,COUNT(*)

FROM EMP Kabir

GROUP BY DEPTID;

Q4. To display the TOTAL salary in each DEPTID.

mysql> SELECT DEPTID, SUM(SALARY)

> FROM EMP Kabir

GROUP BY DEPTID;

Q5. To display the maximum salary and minimum salary in each department.

mysql> SELECT DEPTID, MAX(SALARY), MIN(SALARY)

FROM EMP Kabir

GROUP BY DEPTID;
Student & Stream Table
mysql> use Kabirjain

Database changed

mysql> Create the stream_Kabir table

mysql> CREATE TABLE stream_Kabir

s_lD INT(2) PRIMARY KEY,

Stream CHAR(20),

Location CHAR(50));

Query OK, O rows affected, 1 warning (0.19 sec)

mysql> Create the student_Kabir table

mysql> CREATE TABLE student_Kabir(

Ad_No INT(2),

First_Name CHAR(50),

Last_Name CHAR(50),

> Class INT(2),

Section CHAR(I),
s_lD INT(2),

->Date_Of_Birth DATE,

Percentage FLOAT (5,2),

FOREIGN KEY (S_ID) REFERENCES stream(S_ID));

Query OK, O rows affected, 4 warnings (0.06 sec) mysql>


— Insert data into the stream Kabir table

mysql> INSERT INTO stream_Kabir VALUES

(10, 'Science', 'l Block'),

(20, 'Commerce', 'J Block'),

(30, 'Arts', 'Rishi Block');

Query OK, 3 rows affected (0.01 sec)

mysql> Insert data into the student_Kabir table

mysql> INSERT INTO student_Kabir VALUES

(1, 'SWATHI', 'K', 12, 10, '2007-09-20', 95.50),

(2, 'James', 'Ruber', 12, 'B', 20, '2006-05-20', 65.25),

(3, 'Bharath', 'Kumar', 12, 10, '2006-02-01', 88.50),

(4, 'NAVEEN', 'KUMAR', 12, 'A', 30, '2007-06-10', 50.00),


(5, 'Piyush', 'KUMAR', 12, 'A', 30, '2007-01-10', 46.80),

(6, 'Jatin', 'KUMAR', 12, 'A', 30, '2006-06-08', 56.80),

(7, 'himanshi', 'Panchal', 12, 'A', 30, '2007-06-26', 67.00),

(8, 'Tushar', NULL, 12, 'B', 20, '2008-08-02', 59.80);

Query OK, 8 rows affected (0.01 sec)

mysql> Q2) Select all the elements of the student table

mysql> SELECT* FROM student_Kabir; mysql> Q3) Display the

Ad_No, First_Name, Percentage of the students

mysql> SELECT Ad_No, First_Name, Percentage FROM student_Kabir;

mysql> Q4) Display the details of all the students with less than 60 percentage

mysql> SELECT* FROM student_Kabir WHERE Percentage < 60;

mysql> Q5) Display the Ad_No and First_Name of the students who were born after 01/01/2007

mysql> SELECT Ad_No, First_Name FROM student_Kabir WHERE > '2007-01-01';

mysql> Q6) Display the details of the students who were born before 2007

mysql> SELECT* FROM student_Kabir WHERE < '2007-01-01';


mysql> Q7) Display the details of all the students whose percentage is NULL
mysql> SELECT* FROM student_Kabir WHERE Percentage IS NULL;

mysql> Q8) Display the details of all the students whose percentage is not NULL

mysql> SELECT* FROM student Kabir WHERE Percentage IS NOT NULL;

mysql> Q9 & QIO) Display the various sections of the students, each section appearing only once

mysql> SELECT DISTINCT Section FROM student_Kabir;

mysql> QII) Display the details of all the students born after 2006 and have a percentage of more
than
50

mysql> SELECT * FROM student_Kabir WHERE > '2006-12-31' AND Percentage > 50;
mysql> Q12) Display the First_Name and Percentage of all the students whose section is not 'A'

mysql> SELECT First_Name, Percentage FROM student_Kabir

WHERE Section

mysql> — Q13) Display the First_Name and Section of all the students whose percentage is less than
70 or whose S ID is 10

mysql> SELECT First_Name, Section

FROM student Kabir

WHERE Percentage < 70 OR s_lD = 10;


mysql> Q14) Display the details of the students who were born in the year 2006

mysql> SELECT *

FROM student Kabir

WHERE YEAR(Date_Of_Birth) = 2006;

mysql> — Q15) Display the details of all the students whose percentage is in the range of 50 to 70

mysql> SELECT *

FROM student Kabir


WHERE Percentage BETWEEN 50 AND 70;

mysql> — Q16) Display all the details of the students whose DOB is 2007 and class is 11

mysql> SELECT * FROM student_Kabir

WHERE YEAR(Date_Of_Birth) = 2007 AND Class = 11

mysql> Q17) Display the First_Name and Class for all the students whose section is 'A' or 'B'

mysql> SELECT First_Name, Class

FROM student Kabir

WHERE Section IN ('A', 'B');


mysql> Q18) Display the details of all the students whose percentage is in the range of 50 to 80

mysql> SELECT *

FROM student Kabir

WHERE Percentage BETWEEN 50 AND 80;

mysql> Q19) Display the details of all the students who were born between 2006 and 2007 mysql>
SELECT *

FROM student Kabir

WHERE Date Of Birth BETWEEN '2006-01-01' AND '2007-12-31';

mysql> Q20) Display the Ad_No, First_Name, and Section of all the students for which the Last_Name
ends with 'Kumar'
mysql>
SELECT Ad_No, First_Name, Section FROM student_Kabir WHERE Last_Name LIKE '%Kumar';
mysql> Q21) Display the Ad_No, First_Name, and Section for all the students for which the
First_Name ends with 'a'

mysql> SELECT Ad_No, First_Name, Section FROM student_Kabir WHERE First_Name LIKE '%a';

mysql> Q22) Display the Ad_No and First_Name from all the students which contains 'a'

mysql> SELECT Ad_No, First_Name FROM student_Kabir WHERE First_Name LIKE '%a%';

mysql> Q23) Display the Ad_No and First_Name from all the students for which the First_Name does
not contain 'e'

mysql> SELECT Ad_No, First_Name FROM student_Kabir WHERE First_Name NOT LIKE '%e%';

mysql> Q24) Display the Ad_No and First_Name from all the students for which the First_Name
contains 'a' as the second last character

mysql> SELECT Ad_No, First_Name FROM student_Kabir WHERE First_Name LIKE '%a_',

mysql> Q25) Display the details of all the students in ascending order of their Percentage

mysql> SELECT * FROM student_Kabir ORDER BY Percentage Asc;

mysql> Q26) Display the details of all the students in descending order of their Date_Of_Birth

mysql> SELECT* FROM student_Kabir

ORDER BY Date Of Birth DESC;


mysql>
mysql> Q27) Display the details of all the students in ascending order of their Date_Of_Birth and
Percentage in descending order

SELECT* FROM student_Kabir

ORDER BY Date_Of_Birth ASC, Percentage DESC;

mysql> Q28) Change the Percentage of Piyush from 46.80 to 62.80

mysql> UPDATE student_Kabir SET Percentage = 62.80 WHERE First_Name = 'Piyush';

mysql> Q29) Increase the percentage by 2 for all the students whose Section is 'C'

mysql> UPDATE student_Kabir SET Percentage = Percentage + 2 WHERE Section = 'C';


mysql> — Q30) Delete the records of all the students whose Date_Of_Birth is before
2007

mysql> DELETE FROM student_Kabir WHERE < '2007-01-01';

mysql> Q31) Delete all the records of the student named 'Tushar' mysql>

DELETE FROM student Kabir WHERE First Name 'Tushar';

mysql> Q32) Add another column Phone_NO of type INT() in the student table

mysql> ALTER TABLE student_Kabir ADD Phone_NO INT;


mysql>

Group By Functions
mysql> QI) Display First Name, Class, Section, Location, DOB, and Stream from each student where
stream contains "c" character and DOB is less than 2008.

SELECT First_Name, Class, Section, Location, Date_Of_Birth, Stream

FROM student Kabir

JOIN stream ON student Kabir.S ID = stream Kabir.S ID

WHERE Stream LIKE ‘%c%’AND Date Of Birth < '2008-01-01';

mysql> Q2) Display the number of records in each stream.

mysql> SELECT Stream, COUNT(*)

FROM student Kabir

JOIN stream ON student Kabir.S ID = stream Kabir.S ID

GROUP BY Stream;

mysql> Q3) Display max (Percentage), min (Percentage), count(*), count(Last_Name), avg (Percentage),
sum (Percentage) in each section.
mysql> SELECT Section,
mysql>
-> MAX(Percentage), MIN(Percentage), COUNT(*), COUNT(Last_Name), AVG (Percentage),
SUM(Percentage)

FROM student Kabir

GROUP BY Section;
mysql> Q4) Display the number of students in each stream with percentage between 50 to 70.

mysql> SELECT Stream,COUNT(*)

FROM student Kabir

JOIN stream ON student Kabir.S ID = stream Kabir.S ID

WHERE Percentage BETWEEN 50 AND 70

GROUP BY Stream;

mysql> Q5) Display the number of students in each stream where the number is greater than 5.

mysql> SELECT Stream, COUNT(*)

FROM student Kabir

JOIN stream ON student Kabir.S ID = stream Kabir.S ID

GROUP BY Stream

HAVING COUNT(*) > 5;

mysql> Q6) Display the number of students in each stream where percentage is greater than 60 and
the number of streams is greater than 2.

mysql> SELECT Stream, COUNT(*)


FROM student Kabir

JOIN stream ON student Kabir.S ID = stream Kabir.S ID->

WHERE Percentage > 60

GROUP BY Stream

HAVING COUNT(*) > 2;

mysql> Q7) Find the error:

mysql> Corrected query:

mysql> SELECT First_Name, Section, COUNT(*)

FROM student Kabir

GROUP BY Section, First Name;

Numeric Functions
mysql> Q8) Round off the percentage to zero decimal places.

mysql> SELECT Ad_No, First_Name, ROUND(Percentage, O) FROM student_Kabir;

mysql> Q9) Display 23472.162738 up to 4 decimal places. mysql> SELECT ROUND (23472.162738, 4);
mysql> QIO) Display 23472.162538 up to the nearest ten's place. mysql> SELECT ROUND
(23472.162538, -1);

mysql> QII) Outputs of the following commands:

mysql> 2) SELECT ROUND (23472.162738, 2); - Output: 23472.16

mysql> 1) SELECT ROUND(23472.162738, 3); Output: 23472.163 mysql> 3) SELECT ROUND


(23472.162738, 1); Output: 23472.2 mysql> 4) SELECT ROUND (23472.162738, 0); Output: 23472
mysql> - 5) SELECT ROUND (23472.162738, -1); Output: 23470 mysql> - 6) SELECT ROUND
(23472.162738, -2); Output: 23500 mysql> 7) SELECT ROUND (23472.162738, -3); - Output: 23000
mysql> - 8) SELECT POW(4, -2); Output: 0.0625

mysql> 9) SELECT POW(IO, 2); Output: 100

mysql> 10) SELECT POW(MOD(14, 3), 3); Output: 1

mysql>

Date Functions
Q12) Display day from DOB in student table.

mysql> SELECT Ad_No, First_Name, DAY(Date_Of_Birth) FROM student_Kabir;

mysql> Q13) Display year from DOB in student table.

mysql> SELECT Ad_No, First_Name, YEAR(Date_Of_Birth) FROM student_Kabir;

mysql> Q14) Display week from DOB in student table. mysql> SELECT Ad_No, First_Name,
WEEK(Date_Of_Birth) FROM student_Kabir;
mysql> Q15) Display day name from DOB in student table. mysql> SELECT Ad_No, First_Name,
DAYNAME(Date_Of_Birth) FROM student_Kabir; mysql> Q16) Display month from
DOB in student table.

mysql> SELECT Ad_No, First_Name, MONTH(Date_Of_Birth) FROM student_Kabir;

mysql> Q17) Display month name from DOB in student table.

mysql>SELECT Ad_No,First_Name ,

MONTHNAME(Date_Of
_Birth) AS Month_Name FROM student Kabir;

mysql> Q18) Display the First_Name of the student who was born on "Tuesday".

mysql>

mysql> SELECT Ad_No, First_Name FROM student_Kabir WHERE DAYNAME(Date_Of_Birth) = '


Tuesday';

Q19) Display the student information whose DOB is between 2007 and 2008.

mysql> SELECT *

FROM student Kabir-> WHERE Date Of Birth BETWEEN '2007-01-01' AND '2008-12- 31';

mysql> Q20) Display the First_Name, Last_Name, and Percentage of the student who was born in
January.

mysql> SELECT First_Name, Last_Name, Percentage FROM student_Kabir WHERE


MONTH (Date_Of_Birth) = 1;

mysql> Q21) Display the student information who was born in week 1.

mysql> SELECT * FROM student_Kabir WHERE WEEK(Date_Of_Birth)=1;

mysql>

String Function
Q22) Display First_Name in upper case and Last_Name in lower case and merge them.

mysql> SELECT CONCAT(UPPER(First_Name,3),”,LOWER(Last_Name)), FROM student_Kabir;

mysql> Q23) Display all characters in First_Name from the 3rd position.

mysql> SELECT SUBSTRING(First_Name, 3), ->FROM student_Kabir;

mysql> Q24) Display number of characters in Last_Name where percentage is greater than 70 and
First Name contains "l".

mysql> SELECT LENGTH(Last_Name), ->FROM student_Kabir ->WHERE Percentage > 70 AND


First Name LIKE’%I%’;

mysql> Q25) Display first 3 characters from the left of First_Name.

mysql> SELECT LEFT(First_Name,3), FROM student_Kabir;


mysql> — Q26) Write the output of the following commands:

mysql> 1) SELECT LENGTH(‘LVM_SCHOOL’);-> Output: 13


mysql> 2) SELECT LENGTH( LTRIM(‘LVM_SCHOOL’));-> Output: 13

rywsql> 3) SELECT LENGTH (RTRIM(‘LVM_SCHOOL”_LVM_School_ ' )); —Output: 13

mysql>

mysql> 4) SELECT LENGTH( TRIM(‘LVM_SCHOOL’)); ->- Output: 13


Q27) Display the position of 'a' substring in First_Name, Last_Name.

mysql> SELECT First_Name, INSTR(First_Name, 'a'), -> Last_Name, INSTR(Last_Name, 'a') AS


Position Last Name -> FROM student Kabir;

mysql> Q28) Write the output of the following commands:

mysql> 1) SELECT First_Name, INSTR(First_Name, 'a'); Output: varies by record

mysql> 2) SELECT Last_Name, INSTR(Last_Name, 'a'); Output: varies by record

mysql>

Cartesian Functions
Q29: Cartesian product to display all records of stream and student tables.

mysql> SELECT* FROM stream_Kabir, student_Kabir;

mysql> Q30: Display the number of columns (Degree) of student, stream, and Cartesian product
tables.

mysql> Display the number of columns (Degree) of student table.


mysql> SELECT COUNT(*)

FROM student Kabir;

mysql> Display the number of columns (Degree) of stream table.

mysql> SELECT COUNT(*)

FROM stream Kabir • mysql> Display the number of columns (Degree) of the Cartesian product

of stream and student tables.

mysql> SELECT COUNT(*)

FROM stream Kabir, student Kabir;

mysql> Q31: Display the number of rows (Cardinality) of student, stream, and Cartesian product.
mysql>

Display the number of rows (Cardinality) of the student table.

mysql> SELECT

FROM student Kabir;

mysql> Display the number of rows (Cardinality) of the stream table.

mysql> SELECT COUNT(*)

FROM stream Kabir;


mysql> Display the number of rows (Cardinality) of the Cartesian product of stream and student
tables.

mysql> SELECT COUNT(*)

FROM stream Kabir, student Kabir;

Join Function
Q32) Display all the records from stream and student table.

mysql> SELECT * FROM student

WHERE student.S ID= stream.S ID;

mysql>

mysql> Q33) Display student name and their stream (like ART, COMMERCE, SCIENCE) from student
and stream table.

mysql> SELECT First_Name, Last_Name, Stream

-> FROM student

mysql> Q34) Display the number of students in each stream (like ART, COMMERCE, SCIENCE) from
student and stream table.

mysql> SELECT Stream, COUNT(*)

-> FROM student

WHERE student.S ID= stream.S ID


GROUP BY Stream;

mysql> Q35) Display the number of students in each stream having percentage more than 50 and
number of records should be higher than 3 (like ART, COMMERCE, SCIENCE) from student and
stream table.

mysql> SELECT Stream, COUNT(*)AS Num_Students

FROM student

JOIN stream ON student.S ID = stream.S ID

WHERE Percentage > 50

GROUP BY Stream

HAVING COUNT(*)> 3;

You might also like