0% found this document useful (0 votes)
4 views7 pages

Name Marks Last Three Characters ID: Students

The document provides SQL queries for various tasks, including retrieving student names based on marks, finding cities with the shortest and longest names, and identifying triangle types based on side lengths. It also includes queries for formatting occupation names and counting occurrences of each occupation. Additionally, it outlines differences between MySQL and Oracle SQL syntax for specific operations.
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)
4 views7 pages

Name Marks Last Three Characters ID: Students

The document provides SQL queries for various tasks, including retrieving student names based on marks, finding cities with the shortest and longest names, and identifying triangle types based on side lengths. It also includes queries for formatting occupation names and counting occurrences of each occupation. Additionally, it outlines differences between MySQL and Oracle SQL syntax for specific operations.
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/ 7

SQL

 Query the Name of any student in STUDENTS who scored


higher than 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.
Oracle MySQL
select name from students select name from students
where marks > 75 where marks > 75
order by substr(name,-3), id; order by right(name,3), id;

 Query the two cities in STATION with the shortest and


longest CITY names, as well as their respective lengths (i.e.:
number of characters in the name). If there is more than one
smallest or largest city, choose the one that comes first when
ordered alphabetically.
Oracle MySQL
SELECT CITY, LENGTH(CITY) SELECT CITY, LENGTH(CITY)
FROM (SELECT CITY,LENGTH(CITY) FROM FROM STATION
STATION WHERE LENGTH(CITY) = (
WHERE LENGTH(CITY) = ( SELECT MAX(LENGTH(CITY)) FROM STATION
SELECT MAX(LENGTH(CITY)) FROM STATION )
) ORDER BY CITY
ORDER BY CITY) LIMIT 1;
WHERE ROWNUM = 1;
SELECT CITY, LENGTH(CITY)
SELECT CITY, LENGTH(CITY) FROM STATION
FROM (SELECT CITY,LENGTH(CITY) FROM WHERE LENGTH(CITY) = (
STATION SELECT MIN(LENGTH(CITY)) FROM STATION
WHERE LENGTH(CITY) = ( )
SELECT MIN(LENGTH(CITY)) FROM STATION ORDER BY CITY
) LIMIT 1;
ORDER BY CITY)
WHERE ROWNUM = 1;
 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.
Oracle MySQL
SELECT DISTINCT city SELECT DISTINCT city
FROM STATION FROM STATION
WHERE LOWER(SUBSTR(city, 1, 1)) NOT IN WHERE LOWER(LEFT(city, 1)) NOT IN ('a', 'e',
('a', 'e', 'i', 'o', 'u') 'i', 'o', 'u')
OR LOWER(SUBSTR(city, -1)) NOT IN ('a', OR LOWER(RIGHT(city, 1)) NOT IN ('a', 'e',
'e', 'i', 'o', 'u'); 'i', 'o', 'u');

 Write a query identifying the type of each record in the


TRIANGLES table using its three side lengths. Output one of the
following statements for each record in the table:
Equilateral: It's a triangle with sides of equal length.
Isosceles: It's a triangle with sides of equal length.
Scalene: It's a triangle with sides of differing lengths.
Not A Triangle: The given values of A, B, and C don't form a
triangle.
Oracle MySQL
select same
case
when a <= 0 or b <= 0 or c <= 0
then 'Not A Triangle'
when a + b <= c or a+c <= b or b+c
<= a then 'Not A Triangle'
when a = b and b = c then
'Equilateral'
when (a = b) or (a = c) or (b = c)
then 'Isosceles'
else 'Scalene'
end as triangleType
from triangles;
 Generate the following two result sets:
1. Query an alphabetically ordered list of all names
in OCCUPATIONS, immediately followed by the first letter of
each profession as a parenthetical (i.e.: enclosed in
parentheses). For
example: AnActorName(A), ADoctorName(D), AProfessorName(
P), and ASingerName(S).
2. Query the number of ocurrences of each occupation
in OCCUPATIONS. Sort the occurrences in ascending order, and
output them in the following format:
3. There are a total of [occupation_count] [occupation]s.
where [occupation_count] is the number of occurrences of an
occupation in OCCUPATIONS and [occupation] is
the lowercase occupation name. If more than one Occupation has
the same [occupation_count], they should be ordered alphabetically.
Note: There will be at least two entries in the table for each type of
occupation.
Oracle MySQL
-- Query 1: Name with profession initial -- Query 1: Name with profession initial
SELECT SELECT
CASE CASE
WHEN Occupation = 'Professor' THEN WHEN Occupation = 'Professor' THEN
Name || '(P)' CONCAT(Name, '(P)')
WHEN Occupation = 'Doctor' THEN Name WHEN Occupation = 'Doctor' THEN
|| '(D)' CONCAT(Name, '(D)')
WHEN Occupation = 'Actor' THEN Name WHEN Occupation = 'Actor' THEN
|| '(A)' CONCAT(Name, '(A)')
WHEN Occupation = 'Singer' THEN Name WHEN Occupation = 'Singer' THEN
|| '(S)' CONCAT(Name, '(S)')
END AS nameProf END AS nameProf
FROM occupations FROM occupations
ORDER BY Name; ORDER BY Name;

-- Query 2: Count by occupation -- Query 2: Count by occupation


SELECT SELECT
CASE CASE
WHEN Occupation = 'Actor' THEN 'There WHEN Occupation = 'Actor' THEN
are a total of ' || COUNT(*) || ' actors.' CONCAT('There are a total of ', COUNT(*), '
WHEN Occupation = 'Doctor' THEN 'There actors.')
are a total of ' || COUNT(*) || ' doctors.' WHEN Occupation = 'Doctor' THEN
WHEN Occupation = 'Professor' THEN CONCAT('There are a total of ', COUNT(*), '
'There are a total of ' || COUNT(*) || ' doctors.')
professors.' WHEN Occupation = 'Professor' THEN
WHEN Occupation = 'Singer' THEN 'There CONCAT('There are a total of ', COUNT(*), '
are a total of ' || COUNT(*) || ' singers.' professors.')
END AS occupation_count WHEN Occupation = 'Singer' THEN
FROM occupations CONCAT('There are a total of ', COUNT(*), '
GROUP BY Occupation singers.')
ORDER BY COUNT(*), Occupation; END AS occupation_count
FROM occupations
GROUP BY Occupation
ORDER BY COUNT(*), Occupation;

SELECT
Name || '(' || SUBSTR(Occupation, 1, 1) || SELECT
')' AS formatted_name CONCAT(Name, '(', LEFT(Occupation, 1), ')')
FROM OCCUPATIONS AS formatted_name
ORDER BY Name; FROM OCCUPATIONS
SELECT ORDER BY Name;
'There are a total of ' || COUNT(*) || ' ' || SELECT
LOWER(Occupation) || 's.' AS summary CONCAT('There are a total of ', COUNT(*), '
FROM OCCUPATIONS ', LOWER(Occupation), 's.') AS summary
GROUP BY Occupation FROM OCCUPATIONS
ORDER BY COUNT(*), Occupation; GROUP BY Occupation
ORDER BY COUNT(*), Occupation;

 You are given a table, BST, containing two


columns: N and P, where N represents the value of a node
in Binary Tree, and P is the parent of N.
Write a query to find the node type of Binary Tree ordered by the
value of the node. Output one of the following for each node:
 Root: If node is root node.

 Leaf: If node is leaf node.

 Inner: If node is neither root nor leaf node.

Oracle MySQL
select N, select N,
CASE CASE
WHEN P IS NULL THEN ' Root' WHEN P IS NULL THEN ' Root'
WHEN N NOT IN (SELECT DISTINCT(P) FROM WHEN N NOT IN (SELECT DISTINCT(P) FROM
BST WHERE P IS NOT NULL) THEN ' Leaf' BST WHERE P IS NOT NULL) THEN ' Leaf'
ELSE ' Inner' ELSE ' Inner'
END AS nodeType END AS nodeType
FROM BST FROM BST
ORDER BY N; ORDER BY N;
Feature MySQL Oracle
Top-N Rows LIMIT n WHERE ROWNUM <= n or FETCH
FIRST n ROWS
String CONCAT(str1, str2) `str1
Concatenation
Auto-increment AUTO_INCREMENT SEQUENCE + TRIGGER or IDENTITY
Date Functions NOW(), CURDATE() SYSDATE, CURRENT_DATE
IF condition IF(condition, true_val, CASE WHEN ... THEN ...
false_val) ELSE ... END
Boolean Type No explicit boolean (0 = false, 1 Uses NUMBER(1) or CHAR(1)
= true)
Case sensitivity Case-insensitive by default (on Case-sensitive (unless configured)
Windows)
String comparison 'abc' = 'ABC' may be TRUE 'abc' != 'ABC' unless
(collation) upper/lower used
Substring SUBSTRING(str, start, len) SUBSTR(str, start, len)
Date Difference DATEDIFF(date1, date2) date1 - date2 (returns number
of days)

You might also like