SlideShare a Scribd company logo
1 of 11
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

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 RSolutionsRSolutions
 
Database queries
Database queriesDatabase queries
Database querieslaiba29012
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | EdurekaEdureka!
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with examplepranav kumar verma
 
Mysql-overview.pptx
Mysql-overview.pptxMysql-overview.pptx
Mysql-overview.pptxTamilHunt
 
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 optimizationVivek Singh
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database ProgrammingAhmed Swilam
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSabrinaShanta2
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSaiMiryala1
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxBhupendraShahi6
 

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

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 

Recently uploaded (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 

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;