1. Student ID (automatic number) Name Gender Age
0001 xw M 18
0002 mc 16 women
0003 ww male 21
0004 xw M 18
Please write SQL statements to achieve the following functions:
In addition to deleting the Student ID (AutoNumber) field, other fields are the same redundant records!
DELETE the FROM table1 the WHERE (Student ID the NOT the IN ( the SELECT MAX (Student ID) AS xh the FROM TABLE1 the GROUP BY name, gender, age))
select a.teaID,a.age count(*) from teacher a,student b,teacher_student c where a.teaID=c.teaID and b.stuID=c.stuID and a.age>40 and b.age>12 group by a.teaID,a.age;
3.sql face questions a statement to query each department total number of people
Premise: a department employee table table b
a table field (
id - the department number
departmentName- department name
)
Field b (
id-- department number
employee- employee name
)
Question: How can a sql statement to query the total number of people in each department
select a.department,count from tA a,tB b where a.id=b.id group by b.id,a,deparment
4. There are three tables, Student table, SC table and the table Course
Student Table: Student ID (Sno), name (Sname), gender (Ssex), Age (Sage) and Department name (Sdept)
Course table: Course No. (Cno), course name (Cname) and credits (Ccredit);
SC table: school (Sno), course number (Cno) and performance (Grade)
Please check the total student's name and course credits using SQL statements
(Note: If you do not pass the course, so this course credit 0)
method 1:
select Sname,sum(Ccredit) as totalCredit from Student,Course,SC where Grade>=60 and Student.Sno=SC.Sno and Course.Cno=SC.Cno group by Sname
Method 2: modification of the xyphoenix
select sname,sum(case when sc.grade<60 then 0 else course.Ccredit end) as totalCredit from Student,sc,course where sc.sno=student.sno and sc.cno=course.cno group by sname
Method 3: modification of the napolun180410
select Sname,SUM(case when Grade<60 then 0 else Ccredit end) as totalGrade FROM SC JOIN Student ON(Student.sno = SC.sno) JOIN Course ON(SC.Cno = Course.Cno) GROUP BY Student.Sname;
-------------------------------------------------------------------------
There are three tables S, C, SC
S (SNO, SNAME) on behalf of (student number, name)
C (CNO, CNAME, CTEACHER) on behalf of (class number, class name, teacher)
SC (SNO, CNO, SCGRADE) on behalf of (student number, course number results)
problem:
1, all students had to find out did not choose the name "Dawn" teacher.
2, list two or more (including two) failed student's name and grade point average.
3, No. 1 course that is learned another lesson learned the No. 2 all the student's name.
Please write the answer, using standard SQL language dialect, too (please specify what dialect).
-----------------------------------------------------------------------------
answer:
S (SNO, SNAME) on behalf of (student number, name)
C (CNO, CNAME, CTEACHER) on behalf of (class number, class name, teacher)
SC (SNO, CNO, SCGRADE) on behalf of (student number, course number results)
select sno,sname from s; select cno,cname,cteacher from c; select sno,cno,scgrade from sc;
1. Identify the problem had all the students did not choose the name "Dawn" teacher.
The first step: Lesson No. seeking dawn teacher taught all classes
select distinct cno from c where cteacher='黎明'
Step two: choose the dawn of all students in teacher numbers
select sno from sc where cno in (
The first step results
)
The third step: Dawn of the teacher did not choose the names of all students
select sname from s where sno not in (
Results of the second
)
which is:
select sname from s where sno not in ( select sno from sc where cno in ( select distinct cno from c where cteacher='黎明' ) )
----------------------------------------------------------------------------
Question 2: List two or more (including two) failed student's name and grade point average.
The first step: more than two failing students learn numbers
select sno from sc where scgrade < 60 group by sno having count(*) >= 2
Step two: the average score for each student
select sno, avg(scgrade) as avg_grade from sc group by sno
The third step: the first step to get a student number corresponding to the names of the students and the average score
select s.sname ,avg_grade from s
join
The first step results
on s.sno = t.sno
join
Results of the second
on s.sno = t1.sno
which is:
select s.sname ,avg_grade from s join (select sno, count(*) from sc where scgrade < 60 group by sno having count(*) >= 2)t on s.sno = t.sno join (select sno, avg(scgrade) as avg_grade from sc group by sno )t1 on s.sno = t1.sno
Wrong wording:
Mistake: Find the average of all points failed the course, not all programs (including qualified) of the average
The order of execution:
Where will first execute the statement, will not meet the selection criteria to filter out records,
And then filtered data group by group according to clause field,
Having clause then used to filter out packets not meet the conditions,
And then the rest of the sort the data show.
select sname, avg_scgrade from s join (select sno, avg(scgrade) avg_scgrade from sc where scgrade < 60 group by sno having count(*) >= 2) t on (s.sno = t.sno); ---------------------------------------------------------------------------- select sno,sname from s; select cno,cname,cteacher from c; select sno,cno,scgrade from sc;
Question 3: No. 1 course that is learned another lesson learned the No. 2 all the student's name.
The first step: No. 1 No. studied science courses
select sno from sc where cno = 1
Step two: learned Student ID No. 2 courses
select sno from sc where cno = 2
The third step: the No. 1 course and learned learned the lessons of school No. 2
select sno from sc where cno =1 and sno in (select sno from sc where cno = 2)
Fourth step: Get Name
select sname from s where sno in ( select sno from sc where cno = 1 and sno in (select sno from sc where cno = 2) )
or:
select sname from s where sno in (select sno from sc where cno = 1) and sno in (select sno from sc where cno = 2) company 公司名(companyname) 编号(id)
LS 6
DG 9
GR 19
employeehired
Company (id) number (number) quarter (fiscalquarter)
6 2 1
9 2 4
19 4 1
1. Identify the primary key of the table: company (id) employeehired (id) + (fiscalquarter)
2. identify the relationship between the tables: foreign key relationship, employeehired (id) reference company (id)
3. seek fourth-quarter hiring employees over the company name:
select companyname from company c join employeehired e on (c.id = e.id) where fiscalquarter = 4;
4. seek from 1-3 quarter had no recruitment of employees from the company name // Similarly 1-4 quarter
select companyname from company where id not in (select distinct id from employeehired where fiscalquarter not in(1,2,3) );
5. seeking employees from recruitment through between 1-4 quarter the company name and the total number of employees in their respective recruitment
select companyname , sum_numhired from company c join ( select sum(numhired) sum_numhired from employeehired group by id ) t on (c.sum_numhired = t.sum_numhired);
- seeking sector who pay the highest ---- here to start using the built-in table scott account
select ename, sal from emp join (select max(sal) max_sal, deptno from emp group by deptno) t on (emp.sal = t.max_sal and emp.deptno = t.deptno);
- ask the average salary for each department level // multi-table joins, subqueries
SELECT DEPTNO, avg_sal, Grade from // taken from the following table, the table must have a field ( SELECT DEPTNO, AVG (SAL) avg_sal from EMP Group by DEPTNO) T the Join salgrade S ON (t.avg_sal and S BETWEEN s.losal .hisal);
- averaging salary levels for each department
select deptno, avg(grade) from (select deptno, ename, grade from emp join salgrade s on (emp.sal between s.losal and s.hisal)) t group by deptno;
- What are seeking employees who are managers
select ename from emp where empno in (select distinct mgr from emp );
- not allowed to use set of functions, seeking the highest salaries (interview questions) // very abnormal and unfair unfair
Since the connection: the data table on the left is less than the right of the table of the biggest connection is not // that is simple
select distinct sal from emp where sal not in (select distinct e1.sal from emp e1 join emp e2 on (e1.sal < e2.sal));
- Department seeking the highest average salary department number
select deptno, avg_sal from (select deptno, avg(sal) avg_sal from emp group by deptno) where avg_sal = (select max(avg_sal) from (select avg(sal) avg_sal, deptno from emp group by deptno) );
/////////// Another solution ../////////////////////////////
select deptno, avg_sal from (select deptno, avg(sal) avg_sal from emp group by deptno) where avg_sal = (select max(avg(sal)) from emp group by deptno);
//////// nested set of functions, but only cover layer 2, since the multi-line input, single output //////////
- name of the department seeking the highest average salary department
select dname from dept where deptno = ( select deptno from (select deptno, avg(sal) avg_sal from emp group by deptno) where avg_sal = (select max(avg_sal) from (select avg(sal) avg_sal, deptno from emp group by deptno) ) );
- name of the department lowest level departments averaged salary // too complicated PL SQL
//From the inside out
--- First find the salary level for each employee, and then according to the department finds the average salary level
select avg_grade,deptno from (select avg(grade) avg_grade,deptno ( select grade,empno,deptno from emp e join salgrade s on(e.sal between s.losal adn s.hisal) ) group by deptno )
----complete----
select empname ,avg_grade dept d join (select deptno,avg(grade) as avg_grade from (select deptno,empno,grade from emp e join salgrade s on(e.sal between s.losal and s.hisal) ) group by deptno )t1 on d.depno=t1.deptno;
1. Average Salary: select deptno, avg (sal) from emp group by deptno;
2. The average salary level:
select deptno, grade, avg_sal from (select deptno, avg(sal) avg_sal from emp group by deptno) t join salgrade s on ( t.avg_sal between s.losal and s.hisal);
3. The average salary of the lowest grade:
select min (grade) from ( select deptno, grade, avg_sal from (select deptno, avg(sal) avg_sal from emp group by deptno) t join salgrade s on ( t.avg_sal between s.losal and s.hisal) );
4. The average salary of the lowest grade of Department: 2. Connect the display department table dept
select dname, t1.deptno, grade, avg_sal from // deptno 未明确定义列 (select deptno, grade, avg_sal from (select deptno, avg(sal) avg_sal from emp group by deptno) t join salgrade s on ( t.avg_sal between s.losal and s.hisal) ) t1 join dept on (t1.deptno = dept.deptno) where t1.grade = ( select min (grade) from ( SELECT DEPTNO, Grade, avg_sal from ( SELECT DEPTNO, AVG (SAL) avg_sal from EMP Group by DEPTNO) T the Join salgrade S ON (t.avg_sal BETWEEN s.losal and s.hisal) ) ); // has completely repeated The place
::::::: create a view, the view is the table, the subquery: virtual table, link ::::::::
create view v$_dept_avg_sal_info as select deptno, grade, avg_sal from (select deptno, avg(sal) avg_sal from emp group by deptno) t join salgrade s on ( t.avg_sal between s.losal and s.hisal);
// view has been created;
///////// can not build the table, insufficient permissions
conn sys/10023 as sysdba;
grant create table, create view to sctt;
///////// default is to build a table;
select * from v$_dept_avg_sal_info;
5. Simplification
select dname, t1.deptno, grade, avg_sal from v$_dept_avg_sal_info t1 join dept on (t1.deptno = dept.deptno) where t1.grade = ( select min (grade) from v$_dept_avg_sal_info );
- seeking managers among the lowest average salary of a department name (Questions)
- seeking even higher than the highest salary of the general staff of the manager's name
1.select distinct mgr from emp; //king'mgr is null; 2.select max(sal) from emp where empno not in (select distinct mgr from emp where mgr is not null); 3.select ename from emp where empno in (select distinct mgr from emp where mgr is not null) and sal > ( select max(sal) from emp where empno not in (select distinct mgr from emp where mgr is not null) );
- seeking the highest salary of the top 5 employees
- seeking the highest paid 6 to 10 employees (master key)
- Exercise: Find the final entry of five employees
- face questions: To compare the efficiency
select * from emp where deptno = 10 and ename like '%A%'; select * from emp where ename like '%A%' and deptno = 10;
//////// numbers wrong, do not read back, the comparative figures for the first fast; // might have to optimize Oracle
// CSDN - Specialist Clinic MS-SQL Server
::::::::::::::::::::::::::::::::::: homework :::::::::::: :::::::::::::::::::::::::::::::
A simple table TABLE more than 100 messages, including:
Quantity Color
Products 1 red 123
Products 1 126 blue
Product 2 103 blue
Product 2 red NULL
Product 2 Red 89
Products 1 red 203
…………………………
Please complete SQL statement about the problem: There is no primary key
1. by product category, list only the difference between the number of trade names and all kinds of goods in the red than blue:
2. by product category, the statistical data show the following manner
Product Red Blue