SlideShare a Scribd company logo
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 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 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 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 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 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 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 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 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 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 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;

More Related Content

Similar to SQL commands.docx

Chapter08
Chapter08Chapter08
Chapter08
sasa_eldoby
 
MS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutionsMS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutions
RSolutions
 
Sql (DBMS)
Sql (DBMS)Sql (DBMS)
Sql (DBMS)
Saransh Vijay
 
Database queries
Database queriesDatabase queries
Database queries
laiba29012
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
Edureka!
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
pranav kumar verma
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
SAIFKHAN41507
 
Dbms record
Dbms recordDbms record
Dbms record
Teja Bheemanapally
 
Oracle
OracleOracle
Sql
SqlSql
Mysql-overview.pptx
Mysql-overview.pptxMysql-overview.pptx
Mysql-overview.pptx
TamilHunt
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库
renguzi
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
Vivek Singh
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
Sherif Gad
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
Ahmed Swilam
 
1 z0 047
1 z0 0471 z0 047
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SabrinaShanta2
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SaiMiryala1
 
Lab
LabLab
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
BhupendraShahi6
 

Similar to SQL commands.docx (20)

Chapter08
Chapter08Chapter08
Chapter08
 
MS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutionsMS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutions
 
Sql (DBMS)
Sql (DBMS)Sql (DBMS)
Sql (DBMS)
 
Database queries
Database queriesDatabase queries
Database queries
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Dbms record
Dbms recordDbms record
Dbms record
 
Oracle
OracleOracle
Oracle
 
Sql
SqlSql
Sql
 
Mysql-overview.pptx
Mysql-overview.pptxMysql-overview.pptx
Mysql-overview.pptx
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
1 z0 047
1 z0 0471 z0 047
1 z0 047
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
Lab
LabLab
Lab
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 

Recently uploaded

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 

Recently uploaded (20)

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 

SQL commands.docx

  • 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;