Multi-table query, left and right links, etc.

   A while ago, I briefly reviewed SQL due to the need for work, and now I send the summary written before here. Very basic stuff, please correct me if I make mistakes.
First, the table structure:
create table USER_ID
(
 ID number(10),
 name varchar2(255),
 age number(3),
 telephone number(11),
 birthday varchar2(8)
);

create table school
(
  school_id number(10),
  name varchar2(255),
  address varchar2(255),
  level1 number(1),
  is_985 varchar2(1),
  createdate date
);

create table student
(
  ID number(10),
  student_id number(10),
  school_id number(10),
  enter_date date
);

create table course
(
 school_id number(10),
 course_id number(10),
 course_name varchar2(255),
 teacher varchar2(255)
);

create table grade_core
(
 school_id number(10),
 course_id number(10),
 student_id number(10),
 grade_core number(10),
 term varchar2(10)
)

2. Questions and sql:
1. Required output: student number, student name, age, course name, score, semester;
Select student.student_id,User_id.Name,user_id.age,course.course_name,grade_core.grade_core,grade_core.term
from User_Id,student,grade_core,course
where User_Id.id=student.id
    and student.student_id=grade_core.student_id
    and grade_core.course_id=course.course_id
and course.school_id=student.school_id

Among them: grade_core.student_id is not unique, so add the last and

Left link:
select us.ID,g.STUDENT_ID,us.NAME,us.age,g.COURSE_NAME,g.GRADE_CORE,g.TERM
from grade_core g

left join COURSE c
on g.COURSE_ID=c.COURSE_ID

left join student st
on st.STUDENT_ID=g.STUDENT_ID
and st.SCHOOL_ID=g.SCHOOL_ID

left join user_id us
on st.ID=us.ID


2 Find the average grade of the first semester of liming,
   select avg(grade_core.grade_core)
               from user_id,student,grade_core
               where user_id.id=student.id
               and student.school_id=grade_core.school_id
               and grade_core.term=1
               and user_id.name='liming'

    left:select avg(g.grade_core)
       from grade_core g
       left join student stu
       on stu.student_id=g.student_id
       left join user_id us
       on us.id=stu.id
       where g.term=1
       and us.name='liming'
       

3 Find the total number of students in Tsinghua University
       select count(student.id)
       from student,school
       where school.name='Tsinghua'
       and student.school_id=school.school_id

4. Find the average grade of each person for each semester
select avg(g.grade_core)
        from grade_core g,student s,user_id u
        group by g.student_id,g.term


Left link, right link understanding.
select * from grade_core g
left join student st
on g.SCHOOL_ID = st.school_id
and g.Student_id=st.student_id
and g.STUDENT_ID=22201—this one of the left link, if there is one, must this result appear?
where id='102';
 

select * from grade_core g
left join student st
on g.SCHOOL_ID = st.school_id
and g.Student_id=st.student_id
where g.STUDENT_ID=22201 - Find the intersection of this condition and the left link above?
and id='102'
 

Feel the difference between the two


select * from grade_core g
left join student st
on g.SCHOOL_ID = st.school_id
and g.Student_id=st.student_id
and g.STUDENT_ID=22201;
The previous sentence is more often written as:
select * from grade_core g
left join student st
on g.SCHOOL_ID = st.school_id
and g.Student_id=st.student_id
 and st.STUDENT_ID=22201


 

select * from grade_core g
left join student st
on g.SCHOOL_ID = st.school_id
and g.Student_id=st.student_id
where g.STUDENT_ID=22201
 

You can split it into this first to see what is the difference between the results.
First , select * will check out all the fields of the two tables of the left link, link and form an intermediate table.
The and after the left join on restricts the linking process of the tables, and only limits the right table of the middle table, but does not affect the left table; and the and after the where limits the middle table formed after the link.


When writing associations in the future, try to use left join to be more intuitive, which is convenient for sorting out the logic and checking whether the association is correct. where X=X, for association, it is in the form of inner join, which takes the intersection, which cannot meet all business statistical requirements.


If you have time today, take a look. There are five associations: left join, right join, inner join, full join, and cross join.
Full join: Returns the union of the left and right links.
Cross join: Returns the Cartesian product, returning all possible combinations of rows from both tables.
create table grade_core_lsj
(
 course_id number(10) ,
 student_id number(10), primary key (course_id,student_id),
 school_id number(10) not null,
 grade_core number(10)not null,
 term varchar2(10)not null,
 student_name varchar(20)
)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326294685&siteId=291194637