Sql practice
employee表 数据准备
use tempdb
go
if OBJECT_ID('employee') is not null
drop table employee
;with employee(id,name,salary,manager_id) as
(
select * from
(
values
(1,'John',300,3),
(2,'Mike',200,3),
(3,'Sally',550,4),
(4,'Jane',500,7),
(5,'Joe',600,7),
(6,'Dan',600,3),
(7,'Phil',550,NULL)
) as ve(id,name,salary,manager_id)
)
select * into employee from employee
--1.Give the names of employees, whose salaries are greater than their immediate managers':
SELECT e.name FROM employee AS e JOIN employee AS m ON e.manager_id = m.id WHERE e.salary > m.salary
--2.What is the average salary of employees without direct reports
--method1 SELECT Avg(e.salary) AS avgsalry FROM employee AS e LEFT JOIN employee AS m ON m.manager_id = e.id WHERE m.id IS NULL --method2 SELECT Avg(e.salary) AS avgsalary FROM employee AS e WHERE NOT EXISTS (SELECT * FROM employee AS m WHERE m.manager_id = e.id)
/******************************************************************************************/
第二题的数据准备:student course courseSelection 三张表
if OBJECT_ID('student','u') is not null
drop table student
if OBJECT_ID('course','u') is not null
drop table course
if OBJECT_ID('courseSelection','u') is not null
drop table courseSelection
;with student(student_no,student_name) as
(
SELECT * FROM
(values
(1,'John'),
(2,'Mike'),
(3,'Sally'),
(4,'Jane'),
(5,'Joe'),
(6,'Dan'),
(7,'Phil')
) as vstudent(student_no,student_name)
)
select * into student from student
;with course (Course_no,Course_name,Course_teacher,Course_credit) as
(
SELECT * FROM (
VALUES
(1,'Java','Steve',12),
(2,'SQLServer','Bill',8),
(3,'Windows','Robert',16),
(4,'Art','Evan',7),
(5,'C#','Steve',9),
(6,'HTML','Robert',12),
(7,'Finance','Tom',9)
) as vcourse(Course_no,Course_name,Course_teacher,Course_credit)
)
select * into course from course
;with CourseSelection(student_no,Course_no,Grade) as
(
select * from
(values
(3,3,57),
(3,3,52),
(3,3,59),
(3,6,57),
(3,6,75),
(6,2,89),
(1,3,93),
(1,6,88),
(6,7,88),
(6,1,99)
) as vcs (student_no,Course_no,Grade)
)
select * into CourseSelection from CourseSelection
--1.Find the students name who pass both "Finance" and "SQLServer" and their average grade(pass means "grade" >= 60).
SELECT DISTINCT s.student_name, cs.avggrade FROM student AS s JOIN (SELECT Avg(grade) OVER( partition BY student_no) AS avggrade, * FROM courseselection) AS cs ON s.student_no = cs.student_no JOIN course AS c ON cs.course_no = c.course_no WHERE c.course_name IN ( 'SQLServer', 'Finance' ) AND cs.grade >= 60
--2.Find the students name who failed one course more than 3 times and current still not passed.
--max(grade) <60 and group by course_no having count(*)>=3
SELECT s.student_name FROM student AS s JOIN (SELECT student_no FROM courseselection AS cs GROUP BY student_no, course_no HAVING Count(*) > 2 AND Max(grade) < 60) AS cs ON cs.student_no = s.student_no
--3.Update teacher "Tom" 's grade for everyone, for those grade >= 90, deduct 10, for those grade between 65 and 89, deduct 5, the rest remain.
UPDATE cs SET cs.grade = CASE WHEN cs.grade > 90 THEN cs.grade - 10 WHEN cs.grade BETWEEN 65 AND 89 THEN cs.grade - 5 ELSE cs.grade END FROM course AS c JOIN courseselection AS cs ON c.course_no = cs.course_no WHERE c.course_teacher = 'Tom'
--4.Find the average grade each teacher give to their students, sort the result by descending,
-- if one student attend one course more than once, only take the highest grade into account.
SELECT c.course_teacher, Avg(cs.grade) AS avgGrade FROM course AS c JOIN (SELECT course_no, Max(grade) AS grade FROM courseselection GROUP BY student_no, course_no) AS cs ON c.course_no = cs.course_no GROUP BY c.course_teacher ORDER BY avggrade DESC
--5. Find the student names who is qualify to graduate with following conditions:
--a. Total earn course_credit >= 50
--b. Failed no more than 5 courses
--c. The maximum of course_credit from one teacher is 20.(one teacher may have more than one courses)
SELECT s.student_name FROM student AS s JOIN (SELECT student_no, Sum (CASE WHEN totalcreditfromoneteacher > 20 THEN 20 ELSE totalcreditfromoneteacher END) AS TotalCredit FROM (SELECT student_no, course_teacher, Sum (CASE WHEN cs.grade > 60 THEN c.course_credit ELSE 0 END) AS TotalCreditFromOneTeacher FROM course AS c JOIN courseselection AS cs ON cs.course_no = c.course_no GROUP BY student_no, course_teacher) AS A GROUP BY student_no) AS cs ON cs.student_no = s.student_no JOIN (SELECT DISTINCT student_no FROM courseselection cs GROUP BY student_no, course_no HAVING Count(DISTINCT course_no) < 5) AS stuentfaillessthan5courses ON s.student_no = stuentfaillessthan5courses.student_no WHERE cs.TotalCredit>50
Sql practice的更多相关文章
- Sql Practice 2
之前写了一个SP用来向dimention table插入0 -1 dummy row的值,但今天在process adventureworksdw2008示例 数据库的时候报错,查看了一下,是因为自己 ...
- 历经15个小时,终于评出这8本最受欢迎的SQL书籍
文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 来源 | 程序员书库(ID:OpenSourceTop) 原文链接 | https://www.lif ...
- Atitit 数据存储视图的最佳实际best practice attilax总结
Atitit 数据存储视图的最佳实际best practice attilax总结 1.1. 视图优点:可读性的提升1 1.2. 结论 本着可读性优先于性能的原则,面向人类编程优先于面向机器编程,应 ...
- The Practice of .NET Cross-Platforms
0x01 Preface This post is mainly to share the technologies on my practice about the .NET Cross-Platf ...
- 谈一谈SQL Server中的执行计划缓存(上)
简介 我们平时所写的SQL语句本质只是获取数据的逻辑,而不是获取数据的物理路径.当我们写的SQL语句传到SQL Server的时候,查询分析器会将语句依次进行解析(Parse).绑定(Bind).查询 ...
- Partitioning & Archiving tables in SQL Server (Part 1: The basics)
Reference: http://blogs.msdn.com/b/felixmar/archive/2011/02/14/partitioning-amp-archiving-tables-in- ...
- 可输出sql的PrepareStatement封装
import java.io.InputStream; import java.io.Reader; import java.net.URL; import java.sql.Connection; ...
- sql是如何执行一个查询的!
引用自:http://rusanu.com/2013/08/01/understanding-how-sql-server-executes-a-query/ Understanding how SQ ...
- C#读写SQL Server数据库图片
效果图: 下载链接: http://download.csdn.net/detail/u010312811/9492402 1.创建一个Winform窗体,窗体分为“数据上传”和“数据读取”两部分: ...
随机推荐
- 簡單SQL存儲過程實例
簡單SQL存儲過程實例 摘自:http://blog.csdn.net/libra6956/article/details/5589173 实例1:只返回单一记录集的存储过程. 银行存款表(bankM ...
- ASP.Net中Session失效的一种编程思路
在写一个客户的B/S结构应用程序时,突然发现一个技巧,不知道是否是MS的一个BUG,给相关的有研究的朋友原先考虑写一个检查Session的类,Session失效后,必须转向登陆页面,可每一个调用该类的 ...
- MVC 5 + EF6 入门完整教程14 -- 动态生成面包屑导航
上篇文章我们完成了 动态生成多级菜单 这个实用组件. 本篇文章我们要开发另一个实用组件:面包屑导航. 面包屑导航(BreadcrumbNavigation)这个概念来自童话故事"汉赛尔和格莱 ...
- [转]基于 Quercus 的手游项目终于上线了
原文:http://blog.andsky.com/quercus-php-ngame/ 经过半年的开发,我们第一款手游戏终于开发完毕,架构使用了 netty + Quercus 实现用 php 通过 ...
- JPA(5)使用二级缓存
jpa的缓存分为一级缓存和二级缓存,一级缓存值得是会话级别的,而二级缓存是跨会话级别的. 使用二级缓存,使用到了Ehcache,首先第一步需要在配置文件中配置使用了二级缓存 <shared-ca ...
- 彻底卸载JDK的-并只依赖配置环境安装JDK(不依赖注册表)-解决Error opening registry key'software\Javasoft\Java Runti问题
一.备份安装好的绿色版JDK a.重新安装JDK到任意目录,假设这个目录是C:\java.b.将装好的JDK,JRE拷贝到任意一个其他目录,如D:\bak,这样做的目的主要是为了备份JDK.(建议打成 ...
- spring mvc绑定复杂对象报错“Could not instantiate property type [com.ld.net.spider.pojo.WorkNode] to auto-grow nested property path: java.lang.InstantiationException: com.ld.net.spider.pojo.WorkNode”
解决方法之一: 1.确保所有的Pojo都包含了默认的构造器:
- AutoMapper映射ExpressionTree
问题描述 项目中使用AutoMapper进行VO&DTO&Entity的互相映射,但是默认Map方法不支持Expression的转换.如 Expression<Func<E ...
- EntityFramework学习
本文档主要介绍.NET开发中两项新技术,.NET平台语言中的语言集成查询技术 - LINQ,与ADO.NET中新增的数据访问层设计技术ADO.NET Entity Framework.ADO.NET的 ...
- cl_gui_cfw=>flush
用法一: REFRESH_TABLE_DISPLAY虽然刷新的界面,但是SAP GUI并不是实时更新,而是将更新的结果放在缓存中,手动调用CL_GUI_CFW=>FLUSH才能触 ...