DBMS Record
DBMS Record
DBMSRECORD
.1.Query the NAME and COUNTRYCODE of CITY Table where ID is greater than
3000
Select NAME,COUNTRYCODE from city where id>3000;
2.Query all attributes of TamilNadu district in the CITY table.The district code for
TamilNadu is TN..
6.Query all attributes of every Indian city in the CITY table.The COUNTRYCODE
for India is IND.
Select NAME, COUNTRYCODE from CITY WHERE ID > 3000 AND ID<4000;
12.Query the list of CITY name sending with vowels(i.e.,a,e,i,o,u) from STATION.
Your result cannot contain duplicates.
13.Query the list of CITY names from STATION which have vowels (i.e.,a,e, i, o,
and u) as first characters. Your result cannot contain duplicates.
15.Query the list of CITY names from STATION that donot end with vowels. Your
result cannot contain duplicates.
Select DISTINCT CITY from station where city not like '%a' AND city not
like '%e' AND city not like '%i' AND city not like '%o' AND city not like
'%u';
16.Query the list of CITY names from STATION that do not start with vowels.
Your result cannot contain duplicates.
Select DISTINCT CITY FROM STATION WHERE LEFT (CITY,1) NOT IN ('a','e','i','o','u');
17.Query the table of all American cities in CITY with populations larger than
120000. The CountryCode for America is USA.
Select * FROM CITY WHERE POPULATION > 120000 AND COUNTRYCODE='USA';
18.Query the list of CITY names from STATION that either do not start with
vowels or do not end with vowels. Your result cannot contain duplicates.
Select DISTINCT CITY FROM STATION WHERE LEFT (CITY,1) NOT IN ('a','e','i','o','u')
OR RIGHT(CITY,1) NOT IN ('a','e','i','o','u');
.
19.Query the ID, NAME and COUNTRY CODE of CITY Table where ID is
greater than 3000 and less than 4000.
Select ID, NAME , COUNTRYCODE FROM CITY WHERE ID > 3000 AND ID < 4000;
20.Query a list of CITY names from STATION with even ID numbers only. You
may print the results in any order, but must exclude duplicates from your answer.
select DISTINCT CITY FROM STATION where ID % 2 = 0;
21.Query the Name of any student in STUDENTS who scored higher than 75
Marks.Order your output by the last three characters of each name. If two or more
students both have names ending in the same last three characters (i.e.: Bobby,
Robby, etc.), secondary sort them by ascending ID.
SELECT Name FROM STUDENTS WHERE Marks > 75 ORDER BY RIGHT (Name, 3),ID;
22.Write a query that prints a list of employee names (i.e.:the name attribute) from
the Employee table in alphabetical order.
Select NAME from EMPLOYEE ORDER BY NAME;
.
23.Query the list of CITY names from STATION that do not start with vowels and
do not end with vowels. Your result cannot contain duplicates.
Select DISTINCT CITY FROM STATION WHERE LEFT (CITY,1) NOT IN
('a','e','i','o','u') AND RIGHT (CITY,1) NOT IN ('a','e','i','o','u');
24.Write a query that prints a list of employee names (i.e.: the name attribute) for
employees in Employee having a salary greater than $ 2000 per month who have been
employees for less than 10 months. Sort your result by ascending employee_id.
Select NAME FROM EMPLOYEE WHERE SALARY > 2000 AND MONTHS < 10 ORDER
BY EMPLOYEE_ID;
25.Query the list of CITY names starting with vowels (i.e. ,a ,e , i , o, or u)from
STATION. Your result cannot contain duplicates.
Select DISTINCT CITY FROM STATION WHERE LEFT (CITY,1) IN ('a','e','i','o','u');
.
dividethecustomeridby100respectivelyandduly.
customerid+113
quantity*50 price-
50 customerid/100
selectcustomerid+113,quantity*50,price-
50,customerid/100 from items_ordered;
37.Writeaquerythatprints thesum andaverageof the customerid,
quantity and price.
The statement has to return a single result which contains the sum
and average value of customerid,
quantityandpricecolumnsofeverythingreturnedin the
items_ordered table.
select
sum(customerid),sum(quantity),sum(price),avg(customerid),a
vg(quantity),avg(price) from items_ordered;
38.Write a query to display number of rows(Only
count)intheitems_orderedtableNote:UseSQLin- built Function to
find the count
selectcount(*)fromitems_ordered;
39.Write a query that prints the minimum of
customerid,quantityandpriceinitems_orderedtable.
.
Hint:Yourqueryshouldreturnthepriceonly.
selectavg(price)fromitems_ordered;
42.Write a query that prints the minimum and
maximumofcustomerid,quantityandpricein items_ordered
table.
ASCII(characterorcolumn name)
Syntax:
SELECTNOW()FROMtable_name; Table
Name : emp
Columns
Usenow()function aftersalarycolumninthequery.
selectEID,NAME,AGE,SALARY,NOW()fromemp;
45.MIDFunction:
MIDFunctionhasthreeparameters:
MID(column_name,start,length)fromtable-name; Writea
FUNCTIONtodisplaythe salarycolumnof
.
employeetableusingmid function.
TableName:empColu
mns
TableName:empColu
mns
Note:UCASEfunctionissimilartoUPPERfunction.
UseUPPER Function
selectupper(name)fromemp;
.
47.Writeaquerytoaddthepopulationfiledfromcity table.
Note:UseSUMmethod
selectSUM(population)fromcity;
48.Writeafunctiontoconvertallthenamesinthe column to lower
case.
TableName:empColu
mns
selectlower(name)fromemp;
49.Writeanquerytoexecute thesignfunctioninSQL.
Theuserneedstoimplementroundandsquareroofunctionforthecolumnp
riceinitems_orderedtable.
RoundthepricemethodTWOdigits. Hint:
round(price,2)
selectround(price,2),sqrt(price)fromitems_ordered;
51.The"customers"tablehasfollowingfield
1. customerid
2. firstname
3. lastname
4. city
5. state
select*fromcustomersorderbystate;
52.The"customers"tablehasfollowingfield
1. customerid
2. firstname
3. lastname
4. city
5. state
select*fromcustomersorderbycityasc;
.
53.The"customers"tablehasfollowingfield
1. customerid
2. firstname
3. lastname
4. city
5. state
select*fromcustomersorderbyfirstnameasc;
54.The"city"tableconsistsoffollowingfields: 1.ID
2. NAME
3. COUNTRYCODE
4. DISTRICT
5. POPULATION
select*fromcityorderbypopulation;
55.The"customers"tablehasfollowingfield
1. customerid
.
2. firstname
3. lastname
4. city
5. State
select*fromCUSTOMERSorderbyfirstnamedesc;
56.The"suppliers"tablehasfollowingfield
1. SupplierID
2. SupplierName
3. ContactName
4. Address
5. City
6. PostalCode
7. Country
8.Phone
selectcityfromcustomersunionselectcityfromsuppliers order by
city desc;
.
57.The"city"tableconsistsoffollowingfields: 1.ID
2. NAME
3. COUNTRYCODE
4. DISTRICT
5. POPULATION
selectdistinctcountrycodefromcityorderbyCOUNTRYCODE
desc;
Theitems_orderedtabledescriptionisasfollows:
selectcustomers.customerid, customers.firstname,
customers.lastname, items_ordered.item,
items_ordered.pricefromcustomersJOINitems_ordered
ON customers.customerid = items_ordered.customerid
SELECTIF(GRADES.GRADE>=8,STUDENTS.NAME,
NULL),GRADES.GRADE, STUDENTS.MARKS
FROMGRADES,STUDENTS
WHERESTUDENTS.MARKSBETWEENGRADES.MIN_MARK
AND GRADES.MAX_MARK
ORDERBYGRADES.GRADEDESC,STUDENTS.NAME;
.
InputFormat
Thefollowingtablescontainchallengedata:
SELECTH.HACKER_ID,
.
H.NAME,
COUNT(C.CHALLENGE_ID)ASC_COUNT
FROM HACKERS H
JOINCHALLENGESCONC.HACKER_ID=H.HACKER_ID
GROUP BY H.HACKER_ID, H.NAME
HAVINGC_COUNT=
(SELECTCOUNT(C2.CHALLENGE_ID)ASC_MAX
FROM CHALLENGES AS C2
GROUP BY C2.HACKER_ID
ORDERBYC_MAXDESCLIMIT1)
ORC_COUNTIN
(SELECTDISTINCTC_COMPAREASC_UNIQUE FROM
(SELECT H2.HACKER_ID,
H2.NAME,
COUNT(CHALLENGE_ID)ASC_COMPARE
FROMHACKERSH2
JOINCHALLENGESCONC.HACKER_ID=H2.HACKER_ID
GROUP BY H2.HACKER_ID, H2.NAME) COUNTS
GROUP BYC_COMPARE
HAVING COUNT(C_COMPARE) = 1)
ORDERBYC_COUNTDESC,H.HACKER_ID;
.
select
COUNTRY.CONTINENT,FLOOR(AVG(CITY.POPULATION))
FromCITY
joinCOUNTRYonCITY.CountryCode=COUNTRY.Code group
by COUNTRY.Continent;
.
62.GiventheCITYandCOUNTRYtables,query thesum
ofthepopulationsofallcities wherethe CONTINENT is 'Asia'.
selectSUM(CITY.POPULATION)fromCITYjoinCOUNTRYon
COUNTRY.Code=CITY.CountryCodewhere
COUNTRY.Continent = 'Asia';
63.GiventheCITYandCOUNTRYtables,query
thenamesofallcitieswheretheCONTINENTis Africa.
selectCITY.NAMEfromCITYjoinCOUNTRYon COUNTRY.Code
= CITY.CountryCode where COUNTRY.Continent = 'Africa';
64.WriteaQuerytoJoin thefollowingtablesandfind the number of rows
in the table using count function
Table1=items_ordered Table 2 =
submissions
Table 1 = Difficulty
Table2=Departments
select*fromDifficultyUnionselect*from
Departments;
66.WriteaQuerytoJointhefollowingtables -Use Right Join
2.Wehaveanothertablecalledorderswiththree
.
fields(order_id,supplier_id,andorder_date)
Writethequerytogetthefollowingresultset
selectjoins_suppliers.supplier_id,
joins_suppliers.supplier_name,
orders.order_year from
joins_suppliersLeftOuterJoinorderson
joins_suppliers.supplier_id
=orders.supplier_id;
67.WriteaQuerytoJointhefollowingtables -Use Inner Join
1. Wehaveatablecalledjoins_supplierswithtwo fields
(supplier_id and supplier_name)
Writethequerytogetthefollowingresultset
.
selectjoins_suppliers.supplier_id,
joins_suppliers. supplier_name,
orders.order_yearfromjoins_suppliersInner
Join orders on
joins_suppliers.supplier_id=
orders.supplier_id;
68.Write a query using a join to determine which
itemswereorderedbyeachofthecustomersinthe customers table.
selectcustomers.customerid,
customers.firstname,customers.lastname,
items_ordered.item, items_ordered.price
from customers JOIN items_ordered ON
customers.customerid =
items_ordered.customerid;
.
69.WriteaQuerytoJointhefollowingtables
Table1=Difficulty Table
2 = hackers
select*fromDifficultyUnionAllselect*from hackers;
70.WriteaQuerytoJointhefollowingtables
Table1=submissions Table 2 =
hackers
Select name field from "submission" table and join the "hackers"
table using the common hacker_id
columnfromboththetable(submissionstableand hackers table).
selectjoins_suppliers.supplier_id,joins_suppliers.
supplier_name,
orders.order_yearfromjoins_suppliersInnerJoin orders on
joins_suppliers.supplier_id=orders.supplier_id;
71.Write a query to display all the information of the
employeeswhosesalaryiswithintherange1000and 3000
SELECT*FROMsub_employeesWHEREsalary BETWEEN
(SELECT 1000) AND 3000;
.
select*fromsub_employees;
73.Write a query to display the name ( first name
andlastname)forthoseemployeeswhogetsmore salary than the employee
whose ID is 163
76.Write a query to display the name ( first name and last name ),
salary, department id for those employeeswhoearnsuchamountof
salarywhichis the smallest salary of any of the departments.
SELECT*FROMsub_employeesWHEREsalary
BETWEEN (SELECT MIN(salary)FROM
sub_employees)AND2500;
79.Displayalltheinformationofanemployeewhose id is any of the number
134, 159 and 183.
Tablename=sub_employees
SELECT*FROMsub_employeesWHERE employee_id IN
(134,159,183);
.
80.CreateaVIEWasyourRegistrationNumber_01.
1. ID
2. NAME
3. COUNTRYCODE
Aftercreatingviewfortheabove mentionedfields display the view.
select*fromRA1511003010255_01;
81.CreateaVIEWasyourRegistrationNumber_02.
1. firstname
2. city
3. state
.
select*fromRA1511003010255_02;
82.CreateaVIEWasyourRegistrationNumber_10.
1. country_id
2. country_name
SELECTcountry_id,country_nameFROM
RA1511003010255_10;
83.SELECTcountry_id,country_nameFROM
RA1511003010255_10;
SELECTemployee_id,name,salaryFROM
RA1511003010255_03;
84.CreateaVIEWasyourRegistrationNumber_05.
1. location_id
2. street_address
3. country_id
Aftercreatingviewfortheabove mentionedfields display the view.
SELECTlocation_id,street_address,country_id FROM
RA1511003010255_05;
85.CreateaVIEWasyourRegistrationNumber_08.
1. CODE
2. NAME
3. GNP
Aftercreatingviewfortheabovementionedfields display the view.
SELECTCODE,NAME,GNPFROM
RA1511003010255_08;
86.Create a VIEW as your RegistrationNumber_06.
For example your registration number is RA1511003010255 the create a view as
RA1511003010255_06.
The view should be created for suppliers table for the following fields:
1. SupplierID
2. SupplierName
3. Address
4. Phone
After creating view for the above mentioned fields display the view.
.
Hint:
The view should be created for suppliers table for the following
fields:
1. SupplierID
2. SupplierName
3. Address
4. Phone
After creating view for the above mentioned fields display the
view.
Hint:
The view should be created for Country table for the following
fields:
1. CODE
.
2. NAME
3. GNP
4. INDEPYEAR
5. SURFACEAREA
After creating view for the above mentioned fields display the
view.
Hint:
The view should be created for suppliers table for the following
fields:
1. SupplierID
2. SupplierName
3. ContactName
4. City
After creating view for the above mentioned fields display the
view.
Hint:
RA1511003010255_07;
The view should be created for items_ordered table for the following
fields:
1. item
2. price
After creating view for the above mentioned fields display the view.
Hint:
The view should be created for locations table for the following
fields:
1. location_id
2. street_address
3. country_id
After creating view for the above mentioned fields display the view.
.
Hint:
The view should be created for CITY table for the following fields:
1. ID
2. NAME
3. COUNTRYCODE
After creating view for the above mentioned fields display the view.
Hint:
The view should be created for employee table for the following fields:
1. employee_id
2. name
3. salary
After creating view for the above mentioned fields display the view.
Hint:
.
The view should be created for Customers table for the following fields:
1. firstname
2. city
3. state
After creating view for the above mentioned fields display the view.
Hint:
The view should be created for Country table for the following fields:
1. CODE
2. NAME
3. GNP
After creating view for the above mentioned fields display the view.
Hint:
The view should be created for countries table for the following fields:
1. country_id
2. country_name
After creating view for the above mentioned fields display the view.
Hint:
97. In this task, the user needs to write a query to implement Round, Power
and Square root function.
Note: Use SQL in-built Function to calculate round, power and square root
function for station table.
The user needs to implement round, power and square root function for the
column lat_n and long_w and ID in customers table
Use lat_n value as one parameter "x" value and the "y" as 5.
Note: power(x,y) X= value denoted , lat_n and Y denotes="5"
Use long_w value as one parameter "x" value and the "y" as 7.
Note: power(x,y) X= value denoted , long_w and Y denotes="7"
.
Input Format
98. Write a query using a join to determine which items were ordered
by each of the customers in the customers table.