Review: Show All The Information About Every Student Including Phones But Cities

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 6

Review

1. Show dni, name and age for every student


SELECT dni,name,age
FROM student_information;
2. Show dni, name and age for every student order by name
SELECT dni,name,age
FROM student_information
ORDER BY 2;
3. Show dni, name and age for every student greater than 25 years old.
SELECT dni,name,age
FROM student_information
WHERE age > 25;
4. Show the number of students in the database
SELECT count(*) AS number_of_students
FROM student_information;
5. Show the number of students in the database who are greater than 25 years old.
SELECT count(*) AS students_greater_than_25
FROM student_information
WHERE age > 25;
6. Show the number of phones in the database.
SELECT count(*) AS number_of_phones
FROM studentphone;
7. Show the sum and the average of age, use ALIAS.
SELECT sum(age) AS age_sum, avg(age) AS age_average
FROM student_information;
8. Show only the 3 eldest students
SELECT *
FROM student_information
ORDER BY age DESC
LIMIT 3;
9. Show only the second eldest student
SELECT *
FROM student_information
ORDER BY age DESC
LIMIT 1,1;
10. Show in only one row (using a single select) the lowest age, the highest age, count the
number of students in the database, the average of the age. Use ALIAS.
SELECT min(age) AS age_min, max(age) AS age_max, count(*) AS student_number,
avg(age) AS age_average
FROM student_information;
11. Show the amount of students that are living in the same city (clue: GROUP BY)
SELECT count(*), postal_code
FROM sutdent_information
GROUP BY postal_code;
12. Show all the students that are living in Barcelona
SELECT *
FROM student_information,city
WHERE student_information.postal_code = city.postal_code
AND city='Barcelona';
13. Show all the information about every student including phones but cities

SELECT *

FROM student_information,studentphone
WHERE student_information.reg = studentphone.reg;
14. show all the information about every student including city but phones
SELECT *
FROM student_information,city
WHERE student_information.postal_code = city.postal_code;
15. Show all the information content in the database about the student with dni
= '12345678A' (do not show marks)
SELECT *
FROM student_information,city,studentphone
WHERE
student_information.postal_code = city.postal_code
AND student_information.reg = studentphone.reg
AND dni='12345678A';
16. Show students with phone
SELECT reg
FROM studentphone
WHERE phone IS NOT NULL;

SELECT *
FROM student_information,studentphone
WHERE student_information.reg=studentphone.reg
AND phone IS NOT NULL;
17. Show students' name without phone
SELECT name
FROM student_information,studentphone
WHERE student_information.reg=studentphone.reg
AND phone IS NULL;
18. Show reg and name from all the student that are living in London and are

older than 18.


SELECT reg,name
FROM student_information,city
WHERE student_information.postal_code = city.postal_code
AND city.city='London'
AND age > 18;

String functions: CONCAT, UCASE, UPPER, LCASE, LOWER, TRIM, LTRIM,


RTRIM, LENGTH, RIGHT, LEFT, REPLACE, ASCII, CHAR, SPACE, REPEAT,
STRCMP
Use of [NOT] LIKE, % and _
19. Show all the students whose name starts with 'Ja'

SELECT *

FROM student_information
WHERE name LIKE 'Ja%';
20. Show all the students whose name ends with 'as'
SELECT *
FROM student_information
WHERE name LIKE '%as';
21. Show all the students whose name has the string 'vi' in his name
SELECT *
FROM student_information
WHERE name LIKE '%vi%';
22. Show all the phones that starts with '96'
SELECT *
FROM studentphone
WHERE phone LIKE '96%';
23. Show all the phones that do NOT starts with '965'
SELECT *
FROM studentphone
WHERE phone NOT LIKE '965%';
24. Show all the phones that do NOT ends with '00'
SELECT *
FROM studentphone
WHERE phone NOT LIKE '%00';
25. Show all the phones that has this string inside: '00'
SELECT *
FROM studentphone
WHERE phone LIKE '%00%';
26. Show all the phones that starts with '96', then has a undefined number and

the 4th number is a 5, and its last number is 1.


SELECT *
FROM studentphone
WHERE phone LIKE '96_5%1';
27. Show the complete address in only one field. (use CONCAT)
SELECT concat( street,',',number,' ',postal_code) AS complete_address
FROM student_information;
28. Show the complete address: street,number,postal_code,city in only one field.

SELECT concat( street,',',number,' ',city.postal_code,'-',city) AS


complete_address
FROM student_information,city
WHERE student_information.postal_code=city.postal_code;
Use fo UCASE, UPPER, LCASE, LOWER
29. Show name in Upper case ()

SELECT UPPER(name) FROM student_information;


o
SELECT UCASE(name) FROM student_information;
30. Show name in Lower case ()
SELECT LOWER(name) FROM student_information;
o
SELECT LCASE(name) FROM student_information;
Use of TRIM, LTRIM, RTRIM
31. Show names without blanks in its left side
SELECT LTRIM(name)
FROM student_information;
32. Show names without blanks in its right side
SELECT RTRIM(name)
FROM student_information;
33. Show names neither blanks in its left side nor in its right side
SELECT TRIM(name)
FROM student_information;
Use of LENGTH
34. Show the name's lenght for the student with dni='12345678A'.
SELECT LENGTH(name) AS length_name
FROM student_information
WHERE dni='12345678A';

35. Show the name and age for those students that his age has more than one

digit.
SELECT name,age
FROM student_information
WHERE LENGTH(age)>1;
o
SELECT name,age
FROM student_information
WHERE age>=10;
36. Use 'SUBSTRING(str,n) and SUBSTRING(str,n,m) in two queries. First of
all, look for it to get more information in the reference's manual (string
functions). Explain how does it works.
- select the first 5 characters of every student name
SELECT substring(name,1,5) FROM student_information;
- select the last 5 characters of every student name
SELECT substring(name,-5) FROM student_information;

Use of RIGHT, LEFT and REPLACE


37. Replace the name 'Mike' by 'Michael'.
SELECT REPLACE(name,'Mike','Michael')
FROM student_information;
Note: execute now: select name from student_information;, and observe the
results.
38. Show only the 3 first characters for all the names.
SELECT LEFT(name,3)
FROM student_information;
o
SELECT SUBSTR(name,1,3)
FROM student_information;
39. Show only the 3 last characters of the name of the student 1.
SELECT RIGHT(name,3)
FROM student_information
WHERE reg=1;
o
SELECT SUBSTR(name,-3,3)
FROM student_information
WHERE reg=1;
Request about the following functions and put an example for each one
ASCII(x), CHAR(n), SPACE(n), REPEAT(str,n), STRCMP(str1,str2)
Use of GROUP BY .. HAVING
40. Show how many students live in each city, also the name of the city
SELECT city.city,city.postal_code,count(*) AS student_amount
FROM city,student_information
WHERE city.postal_code=student_information.postal_code
GROUP BY city.postal_code;
41. Show how many students live in each city, show only this amount if it is

greater than 100;


SELECT postal_code,count(*) AS st_amount
FROM student_information
GROUP BY postal_code
HAVING count(*)>100;
o
HAVING st_amount>100;
42. Show the higher 'reg' in each city.

SELECT postal_code, max(reg) AS max_reg


FROM student_information
GROUP BY postal_code;
43. Show the higher and the lower 'reg' in each city only for people older than

20;
SELECT postal_code, max(reg) AS max_reg, min(reg) AS min_reg
FROM student_information
WHERE age>20
GROUP BY postal_code;

You might also like