在SQL领域,试题是检验和提升技能的有效方式。以下是对提供的SQL试题的详细解答: 1. 检索出test_a表中各个id段的年龄合计值,且年龄合计值大于50: ```sql SELECT ID, SUM(YAGE) AS TotalAge FROM TEST_A GROUP BY ID HAVING SUM(YAGE) > 50; ``` 2. 检索出test_a表中所有字段信息,同时添加两个字段,显示同一id内的人数和同一年龄的人数: ```sql SELECT a.*, COUNT(*) OVER (PARTITION BY ID) AS SameIdCount, COUNT(*) OVER (PARTITION BY ID, YAGE) AS SameAgeCount FROM TEST_A a; ``` 3. 检索出test_a表中每个id范围内年龄最大的前两名的人员的所有信息: ```sql WITH MaxAgeRank AS ( SELECT *, RANK() OVER (PARTITION BY ID ORDER BY YAGE DESC) AS AgeRank FROM TEST_A ) SELECT * FROM MaxAgeRank WHERE AgeRank <= 2; ``` 4. 检索出test_a表中的记录,其id在test_b表中出现过2次以上(含2次): ```sql SELECT a.* FROM TEST_A a WHERE EXISTS ( SELECT 1 FROM TEST_B b WHERE a.ID = b.ID GROUP BY b.ID HAVING COUNT(*) >= 2 ); ``` 5. 检索出test_a表中年龄比test_b表平均年龄大的记录总数及平均年龄: ```sql WITH AvgAgeB AS ( SELECT AVG(YAGE) AS AvgAge FROM TEST_B ) SELECT COUNT(*) AS CountOfRecords, AVG(a.YAGE) AS AvgAgeInA FROM TEST_A a JOIN AvgAgeB ON 1=1 WHERE a.YAGE > AvgAgeB.AvgAge; ``` 6. 统计A表中每个月注册的用户数、显示月份和统计的数目,以及统计A表中有姓名相同的用户数、显示姓名和统计的数目: ```sql -- 统计每个月注册的用户数 SELECT TO_CHAR(REGDATE, 'YYYY-MM') AS Month, COUNT(*) AS UserCount FROM A GROUP BY TO_CHAR(REGDATE, 'YYYY-MM'); -- 统计姓名相同的用户数 SELECT NAME, COUNT(*) AS NameCount FROM A GROUP BY NAME HAVING COUNT(*) > 1; ``` 如果需要将相同的记录写入B表: ```sql INSERT INTO B (ID, NAME) SELECT ID, NAME FROM ( SELECT ID, NAME FROM A GROUP BY ID, NAME HAVING COUNT(*) > 1 ) duplicates; ``` 然后保留A表中注册时间最大的记录: ```sql DELETE FROM A WHERE ID NOT IN ( SELECT ID FROM ( SELECT ID, MAX(REGDATE) AS MaxRegDate FROM A GROUP BY ID ) max_dates WHERE A.REGDATE = max_dates.MaxRegDate ); ``` 7. 检索出表A与表B通过CODE关联,同一CODE时只检索出TIME最大的记录: ```sql SELECT A.* FROM A JOIN ( SELECT CODE, MAX(TIME) AS MaxTime FROM A GROUP BY CODE ) max_times ON A.CODE = max_times.CODE AND A.TIME = max_times.MaxTime; ``` 8. 取出STATUS全部为9的ID: ```sql SELECT ID FROM A WHERE STATUS = 9 GROUP BY ID HAVING COUNT(*) = (SELECT COUNT(*) FROM A); ``` 9. 根据企业名称、欠费金额、是否欠费展示表格,这里需要具体的表结构和字段名称,但基本SQL逻辑是筛选和聚合。 10. 展示oid、owner、num之和(Sum_num)以及status状态: ```sql WITH StatusCounts AS ( SELECT oid, owner, SUM(num) AS Sum_num, CASE WHEN COUNT(CASE WHEN status = 1 THEN 1 END) = COUNT(*) THEN '完成' WHEN COUNT(CASE WHEN status = 2 THEN 1 END) > 0 THEN '完成中' ELSE '初始' END AS S_status FROM YourTable GROUP BY oid, owner ) SELECT * FROM StatusCounts; ``` 11. 求出id中按序号升序排列后的后2条数据: ```sql WITH SortedScores AS ( SELECT id, score, ROW_NUMBER() OVER (PARTITION BY id ORDER BY 序号) AS RowNum FROM YourTable ) SELECT id, 序号, score FROM SortedScores WHERE RowNum IN (2, 3) ORDER BY id, 序号; ``` 12. 查询重叠时间段: ```sql WITH OverlappingPeriods AS ( SELECT Employee_sn, StartDate, EndDate, LAG(EndDate, 1, StartDate) OVER (PARTITION BY Employee_sn ORDER BY StartDate) AS PrevEndDate FROM YourTable ) SELECT a.Employee_sn, a.StartDate, GREATEST(a.EndDate, b.EndDate) AS EndDate FROM OverlappingPeriods a JOIN OverlappingPeriods b ON a.Employee_sn = b.Employee_sn AND a.StartDate < b.EndDate AND a.PrevEndDate > b.StartDate; ``` 这些SQL语句展示了数据库查询的各种常见技巧,包括聚合函数、分组、窗口函数、子查询、连接操作以及处理日期和时间等。通过练习这些题目,可以加深对SQL语言的理解和应用能力。
- 粉丝: 8
- 资源: 14
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助