SQL SERVER 2005 高级查询(子查询查询)
--SQL高级程序设计:子查询
use AdventureWorks
GO
SELECT DISTINCT EmployeeID from HumanResources.JobCandidate WHERE EmployeeID IS NOT NULL;
SELECT e.EmployeeID,FirstName,LastName
from HumanResources.Employee e
INNER JOIN Person.Contact c
ON e.ContactID = c.ContactID
WHERE e.EmployeeID IN (SELECT DISTINCT EmployeeID from HumanResources.JobCandidate
WHERE EmployeeID IS NOT NULL)
SELECT DISTINCT EmployeeID from HumanResources.JobCandidate WHERE EmployeeID IS NOT NULL
SELECT e.EmployeeID,FirstName,LastName
from HumanResources.Employee e
INNER JOIN Person.Contact c
ON e.ContactID = c.ContactID
WHERE e.EmployeeID NOT IN (
SELECT DISTINCT EmployeeID
from HumanResources.JobCandidate
WHERE EmployeeID IS NOT NULL
)
SELECT CustomerID,MIN((OrderDate)) AS OrderDate
INTO #MinOrderDates
from Sales.SalesOrderHeader
GROUP BY CustomerID
ORDER BY CustomerID
SELECT o.CustomerID,o.SalesOrderID,o.OrderDate
from Sales.SalesOrderHeader o
inner join #MinOrderDates t
on o.CustomerID = t.CustomerID
and o.OrderDate = t.OrderDate
order by o.CustomerID
DROP TABLE #MinOrderHeaders
SELECT O1.CustomerID,o1.SalesOrderID,o1.OrderDate
from Sales.SalesOrderHeader o1
WHERE o1.OrderDate = (SELECT MIN(o2.OrderDate)
from Sales.SalesOrderHeader o2
WHERE O2.CustomerID = o1.CustomerID)
ORDER BY CustomerID
SELECT c.LastName,ISNULL(CAST((SELECT MIN(OrderDate) from Sales.SalesOrderHeader o
WHERE o.ContactID = c.ContactID) AS VARCHAR),'NEVER RECORD') AS "Order Date"
from Person.Contact c
SELECT e.EmployeeID,FirstName,LastName
from HumanResources.Employee e
INNER JOIN Person.Contact c
ON e.ContactID = c.ContactID
WHERE EXISTS(SELECT DISTI
相关文档:
如果你经常遇到下面的问题,你就要考虑使用SQL Server的模板来写规范的SQL语句了:
SQL初学者。
经常忘记常用的DML或是DDL SQL 语句。
在多人开发维护的SQL中,每个人都有自己的SQL习惯,没有一套统一的规范。
在SQL Server Management Studio中,已经给大家提供了很多常用的现成SQL规范模板。
SQL Server Management ......
Oracle 动态SQL返回单条结果和结果集
1. DDL 和 DML
/**//*** DDL ***/
begin
EXECUTE IMMEDIATE 'drop table temp_1';
EXECUTE IMMEDIATE 'create table temp_1(name varchar2(8))';
end;
/**//*** DML ***/
declare
v_1 varchar2(8);
& ......
低效: (索引失效)
SELECT … from DEPARTMENT WHERE DEPT_CODE IS NOT NULL;
高效: (索引有效)
SELECT … from DEPARTMENT WHERE DEPT_CODE >=0;
(27)总是使用索引的第一个列:
如果索引是建立在多 ......
转自:http://hi.baidu.com/arslong/blog/item/b23307e76252342cb8382001.html
Item01
连接字符串中常用的声明有:
服务器声明:Data Source
、Server
和Addr
等。
数据库声明:Initial Catalog
和DataBase
等。
集成Windows
账号的安全性声明:Integrated
Security
和Trusted_Connection
等。
使用数据库 ......
use AdventureWorks
GO
SELECT c.LastName from Person.Contact c;
SELECT * from HumanResources.Employee e
INNER JOIN HumanResources.Employee m
ON e.ManagerID = m.EmployeeID; n
SELECT ProductID,Name,ProductNumber,ReorderPoint
from Production.Product
where ProductID in( select ProductID fro ......