-- 1.给下面的查询填空(使用<#>标记),以获得其后的结果。
SELECT e.emp_id, e.fname,e.lname,b.`name`
FROM employee e INNER JOIN <1> b
ON e.assigned_branch_id = b.<2>;
结果如下:
--2.编写查询,返回所有非商务顾客的账户ID(customer.cust_type = ‘I‘)、顾客的联邦个人识别号码(customer.fed_id)以及账户所依赖的产品名称(product.name).
SELECT c.fed_id,p.`name`
FROM customer c
INNER JOIN account a
ON c.cust_id = a.cust_id
INNER JOIN product p
ON a.product_cd = p.product_cd
WHERE c.cust_type_cd = ‘I‘;
结果如下:
--3.构建查询,查找所有主管位于另一个部门的雇员,需要获取该雇员的ID、姓氏和名字
SELECT e.emp_id,sup.emp_id
FROM employee e
INNER JOIN employee sup
ON e.superior_emp_id = sup.emp_id
WHERE e.dept_id <> sup.dept_id;
结果如下图所示:
--4.编写一个查询,它返回所有产品名称及基于该产品的帐号(用account表里的product_cd列连接product表),查询结果需要包括所有的产品,及时这个产品没有客户开户
SELECT p.product_cd,p.`name`,a.account_id
FROM product p
LEFT JOIN account a
ON p.product_cd = a.product_cd;
结果如下图所示:
-- 5.利用其他外连接类型重写上面的例子,要求查询结果相同
SELECT p.product_cd,p.`name`,a.account_id
FROM account a RIGHT JOIN product p
ON a.product_cd = p.product_cd;
结果如下图所示:
-- 6.编写一个查询,将account表与indiviual 和business 两个表外连接(通过account.cust_id列)要求结果集中每个账户一行,查询的列有account.account_id、account.product_cd、individual.fname,individual.lname和business.name
SELECT a.account_id, a.product_cd,
i.fname,i.lname,b.`name`
FROM account a LEFT JOIN
business b
ON a.cust_id = b.cust_id
LEFT JOIN individual i
ON a.cust_id = i.cust_id;
结果如下图所示:
-- 7.设计一个子查询,生成集合{1,2,3......100}
SELECT ones.num + tens.num + 1
FROM (
SELECT 0 num UNION ALL
SELECT 1 num UNION ALL
SELECT 2 num UNION ALL
SELECT 3 num UNION ALL
SELECT 4 num UNION ALL
SELECT 5 num UNION ALL
SELECT 6 num UNION ALL
SELECT 7 num UNION ALL
SELECT 8 num UNION ALL
SELECT 9 num
) AS ones CROSS JOIN (
SELECT 0 num UNION ALL
SELECT 10 num UNION ALL
SELECT 20 num UNION ALL
SELECT 30 num UNION ALL
SELECT 40 num UNION ALL
SELECT 50 num UNION ALL
SELECT 60 num UNION ALL
SELECT 70 num UNION ALL
SELECT 80 num UNION ALL
SELECT 90 num
) AS tens;
结果如下图所示:
........