Commit b9e6e4e0 by Matias Ferreira

ejercicios completados

parent 870ec6df
CREATE TABLE "students"(
"student_iid" serial primary key,
"fullname" varchar(256) not null,
"birth_date" date
);
create table "courses" (
"course_iid" serial primary key,
"code" varchar(32) not null,
"name" varchar(128) not null,
"start_date" date not null,
"end_date" date not null
);
create table "enrollments"(
"enrollment_iid" serial primary key,
"course_iid" integer not null,
"student_iid" integer not null,
"enrollment_date" date not null,
"payment_date" date not null,
"paid" boolean not null,
foreign key ("course_iid") references "courses"("course_iid"),
foreign key ("student_iid") references "students"("student_iid")
);
insert into "students"("fullname", "birth_date")
values('juan perez', '10-11-2000'),
('pepa pig', '13-10-2001'),
('jimmy neutron', '03-05-2002'),
('james rodriguez', '08-09-2000'),
('ana frank', '04-07-2000'),
('jorge nitales', '02-03-2004'),
('andrea chipreto', '04-01-2005'),
('natalia monges', '30-11-1995'),
('patricia ramirez', '14-06-1994'),
('luhana gonzales', '07-08-1999');
insert into "courses" ("code", "name", "start_date", "end_date")
values('code001', 'Matematicas', '02-02-2020', '03-05-2022'),
('code002', 'Ingles', '03-02-2020', '03-08-2020'),
('code003', 'Programacion', '07-02-2020', '07-05-2020'),
('code004', 'Quimica', '08-02-2020', '08-05-2020'),
('code005', 'Fisica', '09-02-2020', '09-05-2020');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=1),
(select "student_iid" from "students" where "student_iid"=1),
'06-01-2020', '02-02-2020', 'false');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=1),
(select "student_iid" from "students" where "student_iid"=2),
'06-01-2020', '02-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=1),
(select "student_iid" from "students" where "student_iid"=3),
'06-01-2020', '02-02-2020', 'false');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=1),
(select "student_iid" from "students" where "student_iid"=4),
'06-01-2020', '02-02-2020', 'false');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=1),
(select "student_iid" from "students" where "student_iid"=5),
'06-01-2020', '02-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=2),
(select "student_iid" from "students" where "student_iid"=1),
'06-01-2020', '03-02-2020', 'false');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=2),
(select "student_iid" from "students" where "student_iid"=2),
'06-01-2020', '03-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=2),
(select "student_iid" from "students" where "student_iid"=3),
'06-01-2020', '03-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=2),
(select "student_iid" from "students" where "student_iid"=10),
'06-01-2020', '03-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=2),
(select "student_iid" from "students" where "student_iid"=7),
'06-01-2020', '03-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=3),
(select "student_iid" from "students" where "student_iid"=1),
'06-01-2020', '07-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=3),
(select "student_iid" from "students" where "student_iid"=2),
'06-01-2020', '07-02-2020', 'false');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=3),
(select "student_iid" from "students" where "student_iid"=10),
'06-01-2020', '07-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=3),
(select "student_iid" from "students" where "student_iid"=4),
'06-01-2020', '07-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=3),
(select "student_iid" from "students" where "student_iid"=10),
'06-01-2020', '07-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=4),
(select "student_iid" from "students" where "student_iid"=1),
'06-01-2020', '08-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=4),
(select "student_iid" from "students" where "student_iid"=8),
'06-01-2020', '08-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=4),
(select "student_iid" from "students" where "student_iid"=6),
'06-01-2020', '08-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=4),
(select "student_iid" from "students" where "student_iid"=4),
'06-01-2020', '08-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=4),
(select "student_iid" from "students" where "student_iid"=5),
'06-01-2020', '08-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=5),
(select "student_iid" from "students" where "student_iid"=1),
'06-01-2020', '09-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=5),
(select "student_iid" from "students" where "student_iid"=2),
'06-01-2020', '09-02-2020', 'true');
insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
values( (select "course_iid" from "courses" where "course_iid"=5),
(select "student_iid" from "students" where "student_iid"=9),
'06-01-2020', '09-02-2020', 'true');
-- insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
-- values( select "course_iid" from "courses" where "course_iid"=5,
-- select "student_iid" from "students" where "student_iid"=4,
-- '06-01-2020', '09-02-2020', 'true');
-- insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
-- values( (select "course_iid" from "courses" where "course_iid"=2),
-- (select "student_iid" from "students" where "student_iid"=10),
-- '07-02-2020', '07-02-2020', 'true');
-- insert into "enrollments"("course_iid", "student_iid", "enrollment_date", "payment_date", "paid")
-- values( (select "course_iid" from "courses" where "course_iid"=3),
-- (select "student_iid" from "students" where "student_iid"=8),
-- '06-01-2020', '07-02-2020', 'false');
/*2.1. Hacer un query que liste todos los alumnos mayores a 20 años.*/
SELECT fullname
FROM students
WHERE extract( 'year' FROM age(current_date, birth_date)) >= 20;
/*2.10. Hacer un query que liste los alumnos que van a cursar el curso de código a2020 con 25 años.*/
SELECT fullname
FROM
(
(
enrollments en
LEFT JOIN
courses co
ON en.course_iid = co.course_iid
)
AS en_join_co
LEFT JOIN
students st
ON en_join_co.student_iid = st.student_iid
)
AS en_j_co_j_st
WHERE EXTRACT('year' FROM age(CURRENT_DATE, en_j_co_j_st.birth_date)) >= 25
AND
en_j_co_j_st.code = 'a2020';
/*2.2. Hacer un query que liste los cursos de más de 3 meses de duración.*/
SELECT name
FROM courses
WHERE extract('year' FROM age(end_date, start_date))*12 +
extract('month' FROM age(end_date, start_date)) > 3;
/*2.3. Hacer un query que liste los nombres de los alumnos enrolados al curso de código a1010.*/
SELECT fullname
FROM
(
(
enrollments er
LEFT JOIN
students st
ON er.student_iid = st.student_iid
)
AS er_st
LEFT JOIN
courses co
ON er_st.course_iid = co.course_iid
)
AS er_st_co
WHERE er_st_co.code = 'a1010';
/*2.4. Hacer un query que liste los nombres de los alumnos morosos (que están enrolados a cursos pero que no han pagado aun). La lista no tiene que tener nombres repetidos.*/
SELECT DISTINCT fullname
FROM
(
students st
LEFT JOIN
enrollments er
ON st.student_iid = er.student_iid
)
AS st_er
WHERE st_er.paid = 'false';
/*2.5. Hacer un query que liste los nombres de los alumnos que empiezan algún curso el mes que viene. La lista no tiene que tener nombres repetidos.*/
SELECT fullname
FROM
(
students st
LEFT JOIN
enrollments er
ON er.student_iid = st.student_iid
)
AS er_st
LEFT JOIN
courses co
ON er_st.course_iid = co.course_iid
WHERE EXTRACT('month' FROM start_date) -
EXTRACT('month' FROM CURRENT_DATE) = 1
ORDER BY fullname ;
/*2.6. Hacer un query que liste los nombres de los alumnos que están inscriptos a más de un curso. La lista no tiene que tener nombres repetidos.*/
SELECT fullname
FROM
(
SELECT student_iid
FROM enrollments
GROUP BY student_iid
HAVING count(student_iid) > 1
)
AS X
LEFT JOIN
students st
ON st.student_iid = X.student_iid;
/*2.7. Hacer un query que liste los nombres de los alumnos y los cursos a los que está inscripto, siempre que haya pagado su inscripción el mismo día que se inscribió.*/
SELECT fullname, name
FROM
(
SELECT *
FROM
(
enrollments er
LEFT JOIN
students st
ON er.student_iid = st.student_iid
)
AS er_join_st
LEFT JOIN
courses co
ON er_join_st.course_iid = co.course_iid
)
AS mambru
WHERE payment_date = enrollment_date
ORDER BY fullname, name;
/*2.8. Hacer un query que liste los códigos de los 3 cursos con mayor cantidad de inscriptos.*/
SELECT code, count(*)
FROM enrollments en
LEFT JOIN
courses co
ON en.course_iid = co.course_iid
GROUP BY code
ORDER BY count(*) DESC
LIMIT 3;
/*2.9. Hacer un query que liste los alumnos que están morosos en todos los cursos en los que están inscriptos.*/
SELECT DISTINCT fullname
FROM
(
SELECT *
FROM enrollments
WHERE student_iid IN
(
SELECT *
FROM
(
SELECT student_iid st_id
FROM enrollments
GROUP BY student_iid, paid
ORDER BY st_id
)
AS first
GROUP BY first.st_id
HAVING count(first.st_id) = 1
)
AND paid = 'false'
)
AS elegidos
LEFT JOIN
students st
ON elegidos.student_iid = st.student_iid ;
package com.roshka.jdbc;
import java.sql.*;
public class Q10 {
public static void main( String [] args ) {
String url = "jdbc:postgresql://localhost:5433/testdb";
String user = "test";
String password = "pqntslc";
try {
/* QUERY
* select distinct fullname
* from ( (enrollments en
* left join students st)
* on en.student_iid = st.student_iid) as en_st
* left join courses co
* on en_st.course_iid = co.course_iid) as en_st_co
* where en_st_co.code = 'a1010';
* */
Connection connection = DriverManager.getConnection(url, user,password);
System.out.println("estoy conectado a la base de datos");
Statement st = connection.createStatement();
String query = "SELECT DISTINCT fullname "
+ "FROM ((enrollments en "
+ "LEFT JOIN students st "
+ "ON en.student_iid = st.student_iid) AS en_st "
+ "LEFT JOIN courses co "
+ "ON en_st.course_iid = co.course_iid) AS en_st_co "
+ "WHERE en_st_co.code = 'a1010';";
ResultSet rs = st.executeQuery(query);
while(rs.next() ) {
String fullname = rs.getString("fullname");
System.out.println(fullname);
}
}catch(SQLException e) {
e.printStackTrace();;
}
}
}
package com.roshka.jdbc;
import java.sql.*;
public class Q11 {
public static void main( String [] args ) {
String url = "jdbc:postgresql://localhost:5433/testdb";
String user = "test";
String password = "pqntslc";
try {
/* QUERY
* SELECT name
* FROM courses
* WHERE EXTRACT('week' FROM start_date)
* - EXTRACT('week' FROM CURRENT_DATE) = 1;
* */
Connection connection = DriverManager.getConnection(url, user,password);
System.out.println("estoy conectado a la base de datos");
Statement st = connection.createStatement();
String query = "SELECT name "
+ "FROM courses "
+ "WHERE EXTRACT('week' FROM start_date) "
+ "- EXTRACT('week' FROM CURRENT_DATE) = 1;";
ResultSet rs = st.executeQuery(query);
while(rs.next() ) {
String name = rs.getString("name");
System.out.println(name);
}
}catch(SQLException e) {
e.printStackTrace();;
}
}
}
package com.roshka.jdbc;
import java.util.Scanner;
import java.sql.*;
public class Q12 {
public static void main( String [] args ) {
String url = "jdbc:postgresql://localhost:5433/testdb";
String user = "test";
String password = "pqntslc";
Scanner entrada = new Scanner(System.in);
String codigo;
try {
/* QUERY
* SELECT fullname, name, code, paid
* FROM (enrollments en
* LEFT JOIN courses co
* ON en.course_iid = co.course_iid) AS en_co
* LEFT JOIN students st
* ON en_co.student_iid = st.student_iid
* where paid = 'false' and code=codigo;
* */
Connection connection = DriverManager.getConnection(url, user,password);
System.out.println("estoy conectado a la base de datos");
Statement st = connection.createStatement();
System.out.println("Ingrese el codigo del curso: ");
codigo = entrada.nextLine();
String query = "SELECT fullname, name, enrollment_iid, paid "
+ "FROM (enrollments en "
+ "LEFT JOIN courses co "
+ "ON en.course_iid = co.course_iid) AS en_co "
+ "LEFT JOIN students st "
+ "ON en_co.student_iid = st.student_iid "
+ "WHERE paid = 'false' AND code='"+codigo+"';";
ResultSet rs = st.executeQuery(query);
while(rs.next() ) {
String paid = rs.getString("paid");
String nombre = rs.getString("fullname");
String enrollment_iid = rs.getString("enrollment_iid");
System.out.println("EL alumno "+nombre+" ya ha pagado? 's' o 'n'");
String respuesta = entrada.next();
if(respuesta.equals("s") ) {
String update = "UPDATE enrollments "
+ "SET paid = 'true', "
+ "payment_date = CURRENT_DATE "
+ "WHERE enrollment_iid = '"+enrollment_iid+"';";
st.executeUpdate(update);
System.out.println("se ha actualizado la base de datos");
}
}
System.out.println("termino la actualizacion");
}catch(SQLException e) {
e.printStackTrace();;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment