1. 1 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
MySQL background Theory
1. What is My-SQL? Write different data types used in My-SQL.
2. What are DDL commands? Explain some DDL commands with syntax
and examples. (CREATE, DROP, ALTER, TRUNCATE, RENAME)
3. What are DML commands? Explain some DML commands with syntax
and examples. (INSERT, UPDATE, DELETE)
4. What is SELECT command? Write and explain some examples of
SELECT command
2. 2 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
My-SQL Lab Exercise - I
1. Create a database named "RECORDS" and another database
"TEST".
2. Create a SQL statement to create a simple table "countries"
including columns country_id, country_name and continent
3. Write a SQL statement to create a simple table "students"
including columns roll, first_name, last_name, DateOfBirth and age.
Ans:
CREATE DATABASE RECORDS;
CREATE DATABASE TEST;
Ans:
CREATE TABLE countries (
country_id INT PRIMARY KEY,
country_name VARCHAR(255),
continent VARCHAR(255)
);
Ans: CREATE TABLE students (
roll INT PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255),
DateOfBirth DATE,
age INT
);
3. 3 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
4. Write a SQL statement to insert five records in the table "students"
5. Write a SQL statement to insert five records in the table
"countries".
Ans:
INSERT INTO students (roll, first_name, last_name, DateOfBirth, age)
VALUES
(1, 'John', 'Doe', '1995-05-15', 28),
(2, 'Jane', 'Smith', '1998-08-25', 25),
(3, 'Alice', 'Johnson', '1997-03-10', 26),
(4, 'Bob', 'Brown', '1996-11-20', 27),
(5, 'Eva', 'Williams', '1999-04-05', 24);
Ans:
INSERT INTO countries (country_id, country_name, continent)
VALUES
(1, 'India', 'Asia'),
(2, 'United States', 'North America'),
(3, 'France', 'Europe'),
(4, 'Brazil', 'South America'),
(5, 'South Africa', 'Africa');
4. 4 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
6. Write a SQL statement to create a table named "jobs" including
columns job_id, job_title, min_salary, max_salary and check whether
the max_salary exceeding upper limit 25000.
7. Write a SQL statement to display those countries which lie in the
continent "Asia" from the table "continents"
8. Write a SQL statement to display only full_name and age of all
students from "students" table
Ans:
CREATE TABLE jobs (
job_id INT PRIMARY KEY,
job_title VARCHAR(255),
min_salary DECIMAL(10, 2),
max_salary DECIMAL(10, 2),
CONSTRAINT check_max_salary CHECK (max_salary <= 25000)
);
Ans:
SELECT countries.country_name
FROM countries
JOIN continents ON countries.continent_id = continents.continent_id
WHERE continents.continent_name = 'Asia';
5. 5 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
9. Write a SQL statement to remove the table "jobs"
10. Write a SQL statement to remove the database "TEST"
11. Write a SQL statement to Add a new field "Address"
VARCHAR(50) in the table "employee".
12. Write a SQL statement to remove a field "FirstName"
VARCHAR(50) in the table "employee"
Ans:
SELECT first_name, last_name
FROM students
Ans:
DROP DATABASE TEST;
Ans:
DROP TABLE jobs;
Ans:
ALTER TABLE employee
ADD Address VARCHAR(50);
6. 6 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
13. Write a SQL statement to change the field name "Fname" to
FirstName VARCHAR(50) in the table "employee".
14. To remove all the data from the table "CLASS" without removing
the table structure.
15. To make "StudentID" as primary key of the table "student"
Ans:
ALTER TABLE employee
DROP COLUMN FirstName;
Ans:
ALTER TABLE employee
CHANGE COLUMN Fname FirstName VARCHAR(50);
Ans:
DELETE FROM CLASS;
Ans:
ALTER TABLE student
PRIMARY KEY (StudentID) NOT NULL;
7. 7 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
My-SQL Lab Exercise - II (Using SELECT statement and WHERE
clause)
1. Write a SQL statement to display first_name and last_name of
students where first_name is "Ram"
2. Write a SQL statement to display all the records where first_name
starts with letter "S"
3. Write a SQL statement to display all the records where first_name
ends with letter "S"
Ans:
SELECT first_name, last_name
FROM students
WHERE first_name = 'Ram';
Ans:
SELECT *
FROM students
WHERE first_name LIKE 'S%';
Ans:
SELECT *
FROM students
WHERE first_name LIKE '%S';
8. 8 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
4. Write a SQL statement to display all the records where first_name
contains letter "A"
5. Write a SQL statement to display all the records where age > 25
6. Write a SQL statement to display roll, first_name and last_name
of students where last_name is "Shrestha".
Ans:
SELECT *
FROM students
WHERE first_name LIKE '%A%';
Ans:
SELECT *
FROM students
WHERE age > 25;
Ans:
SELECT roll, first_name, last_name
FROM students
WHERE last_name = 'Shrestha';
9. 9 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
7. Write a SQL statement to change the name of the field "age" to
"student_age".
8. Write a SQL statement to display all the records of "salary" table
where salary ranges between 20000 and 30000
9. Write a SQL statement to delete the records where first_name and
last_name starts with "S"
Ans:
SELECT *
FROM salary
WHERE salary BETWEEN 20000 AND 30000;
Ans:
DELETE FROM students
WHERE first_name LIKE 'S%' AND last_name LIKE 'S%';
Ans:
ALTER TABLE students
RENAME COLUMN age TO student_age;
10. 10 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
10. Write a SQL statement to delete the records where age < 20
11. Write a SQL statement to change all the names "Ram" to
"Shyam"
12. Write a SQL statement to display all the records in ascending
order based on first_name
Ans:
DELETE FROM students
WHERE age < 20;
Ans:
UPDATE students
SET first_name = 'Shyam'
WHERE first_name = 'Ram';
Ans:
SELECT *
FROM students
ORDER BY first_name ASC;
11. 11 Gritika Shrestha Roll no:11 Grade 12 ‘K2’
13. Write a SQL statement to display all the records having "A" as
third letter of the "Fname"
14. Write a SQL statement to display all the records in ascending
order based on last_name and then by "age"
15. Write a SQL statement to display all the records where first_name
is "Shyam" and age = 25.
Ans:
SELECT *
FROM students
WHERE SUBSTRING(Fname, 3, 1) = 'A';
Ans:
SELECT *
FROM students
ORDER BY last_name ASC, age ASC;
Ans:
SELECT *
FROM students
WHERE first_name = 'Shyam' AND age = 25;