USE TSQLFundamentals2008
SELECT empid,YEAR(orderdate) AS orderyear,COUNT(*) numorders
FROM Sales.Orders
WHERE custid=71
GROUP BY empid,YEAR(orderdate)
HAVING COUNT(*)>1
ORDER BY empid,orderyear
USE TSQLFundamentals2008
SELECT empid,YEAR(orderdate) AS orderyear,COUNT(*) numorders
FROM Sales.Orders
WHERE custid=71
GROUP BY empid,YEAR(orderdate)
HAVING COUNT(*)>1
ORDER BY empid,orderyear
如上代码,在SQL中逻辑处理顺序如下:
USE TSQLFundamentals2008
FROM Sales.Orders
WHERE custid=71
GROUP BY empid,YEAR(orderdate)
HAVING COUNT(*)>1
SELECT empid,YEAR(orderdate) AS orderyear,COUNT(*) numorders
ORDER BY empid,orderyear
e.尽量不用SELECT * 形式查询所有列,而尽量用列明; 2.2.6 ORDER BY
a.理解SQL最重要的一点就是要明白表不保证是有序的,因为表是为了代表一个集合(如果有重复项,则是多集),而集合是无序的。这意味着,如果在查询表时不指定一个ORDER BY子句,那么虽然查询可以返回一个结果表,但SQL Server可以自由地按任意顺序对结果张的行进行排序;
--方法1
select distinct studentName
from StudentScores
where studentName not in
(
select distinct studentName
from StudentScores
where courseGrades<=80
)
--方法2
select studentName
from StudentScores
group by studentName
having min(courseGrades)>80
Q2:KEY
DELETE DEMO_DELTE
WHERE ID
NOT IN(
SELECT min(ID)
FROM DEMO_DELTE_2
GROUP BY xuehao,XM,kcbh,kcmc,fs)
Q3:KEY
SELECT team1.TeamName,team2.TeamName
FROM Team team1,Team team2
WHERE team1.TeamName<team2.TeamName
sqlserver查询数据的所有表名和行数
//查询所有表明select name from sysobjects where xtype='u'
select * from sys.tables//查询数据库中所有的表名及行数
SELECT a.name, b.rows
FROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id
WHERE (a.type = 'u') AND (b.indid IN (0, 1))
ORDER BY a.name,b.rows DESC
//查询所有的标明及空间占用量\行数
select
object_name(id) tablename,
8*reserved/1024 reserved,
rtrim(8*dpages)+'kb' used,
8*(reserved-dpages)/1024 unused,
8*dpages/1024-rows/1024*minlen/1024 free,
rows
--,*
from sysindexes
where indid=1
order by tablename,reserved desc