`

SQL题目

    博客分类:
  • sql
阅读更多
SQL

Given 3 tables:

    student(_id, _name, _age, _class)
    course(_id, _name)
    student_course(_student_id, _course_id, _score)




1. find all students who select a course's number 'c2'.
        select _id, _name
        from student_course, student
        where _course_id = 'c2'



2. find all students who select a course named "Java".
        select _id, _name 
        from student 
        where _id in (
            select _student_id
            from student_course sc, course c
            where sc._course_id = c._id 
                  and  c._name = 'Java'
        )


3. find all students who didn't select the course's number 'c5'.
        select _id, _name
        from student
        where _id not in (
            select _student_id
            from student_course
            where _course_id = 'c5'
        )


4. find all the student's who selected at least one course.
        select count(distinct _student_id)
        from student_course


5. find all the student's who selected at least 5 courses.
        select _id, _name
        from student
        where _id in (
            select _student_id
            from student_course
            group by _student_id
            having count(_course_id) > 5
        )

    -- The HAVING clause was added to SQL, because the WHERE keyword could not be used with aggregate functions.

6. find students whose seleted course score all great than 80
        select _id, _name
        from student
        where _id not in (
            select distinct _student_id 
            from student_course
            where _score < 80
        )

    -- the "in/not in" operator will make the inner table left join the outer table.

7. delete redundant student records which are only different in  _id.
        delete
        from student
        where _id no in (
            select min(_id)
            from student
            group by _name, _age, _class
        )


8. list the student name and the number of their total selected courses.
        select _name , count(*)
        from (
            select _name
            from student s
                 right join student_course sc
                 on s._id = sc.student_id
        )
        group by _name



9. How to query table "sale"
    year  month amount
    1991   1     1.1
    1991   2     1.2
    1991   3     1.3
    1991   4     1.4
    1992   1     2.1
    1992   2     2.2
    1992   3     2.3
    1992   4     2.4
   
   To the following result:
    year  m1   m2   m3   m4
    1991  1.1  1.2  1.3  1.4
    1992  2.1  2.2  2.3  2.4 

    select
            year,  
            ( 
                select 
                     amount 
                from sale m 
                where 
                     m.year = sale.year  
                     and  month = 1
            ) as m1, 
            ( 
                select 
                     amount 
                from sale m 
                where 
                     m.year = sale.year  
                     and  month = 2
            ) as m2, 
            ( 
                select 
                     amount 
                from sale m 
                where 
                     m.year = sale.year  
                     and  month = 3
            ) as m3,
            ( 
                select 
                     amount 
                from sale m 
                where 
                     m.year = sale.year  
                     and  month = 4
            ) as m4
    from sale  
    group by year 














-
转载请注明:
原文出处:http://lixh1986.iteye.com/blog/2334132










-


分享到:
评论

相关推荐

    考试常考的SQL题目---无私奉献了!

    这个压缩包文件“考试常考的SQL题目---无私奉献了!”显然是为准备SQL相关考试或提升技能的人们准备的一份宝贵资料。下面我们将深入探讨一些常见的SQL知识点,这些知识点通常会在考试中出现,并且对于理解和操作...

    中兴SQL题目 中兴SQL题目

    中兴SQL题目中提到的第一个查询语句是为了找出每个部门中月工资最高的职工号。这个查询使用了子查询,对于外层的每个职工记录,都需要对内层的职工表进行检索,这可能导致较高的计算开销,特别是当职工表非常大时。 ...

    强网杯SQL题目复现php.zip

    标签中的“强网杯SQL题目复现”再次强调这是为了解决特定竞赛中的SQL挑战。 【压缩包子文件的文件名称列表】: 1. README.md:这是一个Markdown格式的文档,通常包含项目简介、使用说明、安装步骤等内容,对于理解...

    火车销售系统题目sql题目

    【标题】:“火车销售系统题目sql题目” 在IT领域,尤其是数据库管理和数据分析中,SQL(Structured Query Language)是不可或缺的工具。"火车销售系统题目sql题目"这一主题聚焦于使用SQL来解决与火车销售系统相关...

    中兴SQL题目中兴SQL题目

    在中兴SQL题目中,主要涉及到了如何高效地使用SQL查询语言来获取所需数据。以下是对这些题目中所提及知识点的详细解释: 1. **子查询与效率**: - 提供的查询语句是通过子查询来找出每个部门月工资最高的职工号。...

    补充的sql题目.rar

    本压缩包"补充的sql题目.rar"包含了多个与SQL相关的文件,让我们逐一解析这些文件并探讨其中可能涵盖的SQL知识点。 首先,"补充sql题目.doc"很可能是一个文档,包含了各种SQL题目,用于测试和提升用户对SQL的掌握...

    几个很好的SQL题目

    几个很好的SQL题目,都是得好好想一想的,希望大家喜欢

    PostgreSQL DBA认证 PGCE-E-092-中级SQL题目

    "PostgreSQL DBA认证 PGCE-E-092-中级SQL题目" PostgreSQL DBA认证 PGCE-E-092-中级SQL题目 PostgreSQL DBA认证PGCE-E-092-中级SQL题目是一个测试SQL掌握程度的题目,涵盖了PostgreSQL数据库管理系统的各种知识点...

    sql题目答案................

    针对"sql题目答案"这个主题,我们可以探讨一些常见的SQL知识点。 首先,基础的SQL语法包括SELECT语句,用于从数据库中检索数据。例如,如果我们有一个名为"dsdfsd.sql"的文件,可能包含了这样的查询: ```sql ...

    一些简单的sql题目.txt

    这是一本关于sqi题目的文档,相关的有问题,有答案,有一些讲解。希望能够对您有所帮助,谢谢。

    超级经典的SQL题目

    在SQL的世界里,掌握经典题目能有效提升你的技能水平,特别是当你面对的是“超级经典的SQL题目”这样的资源时。这个压缩包显然包含了丰富的学习材料,旨在帮助你全面理解和运用SQL Server 2005的核心概念。以下是...

    sql 题目整合(1)

    在面试中,SQL题目经常被用来评估应聘者对数据库操作的理解和熟练程度。本文将深入探讨SQL的基础概念,常见查询技巧以及一些面试中可能遇到的问题。 一、SQL基础 1. 数据库创建与删除:SQL允许创建(CREATE ...

    程序员必备的SQL题目.zip

    以下是对"程序员必备的SQL题目.zip"这个压缩包文件中可能包含的知识点的详细解释。 1. SQL基础概念: - 数据库(Database):存储数据的容器,如MySQL、PostgreSQL、Oracle等。 - 表(Table):数据库中的数据...

    整理mysql、oracle数据库相关 笔试面试题,主要为了应对面试过程中遇到的sql题目

    整理mysql、oracle数据库相关 笔试面试题,主要为了应对面试过程中遇到的sql题目 、.学生表 Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 2.课程表 Course(CID,...

    13道SQL题目

    13道SQL题目,童鞋们下载后先别看答案自己试着做一下,看ok不?

    sql题目答案.............

    在这个“sql题目答案”主题中,我们将深入探讨一些常见的SQL知识点。 首先,我们要理解SQL的基本查询语句——SELECT。SELECT语句用于从一个或多个表中获取数据,例如: ```sql SELECT column1, column2 FROM table...

    持续更新, 整理mysql、oracle数据库相关 笔试面试题,主要为了应对面试过程中遇到的sql题目

    在IT行业中,数据库是至关重要的组成部分,特别是在存储和管理数据方面。MySQL和Oracle是两种广泛使用的数据库管理系统(DBMS),...不断地学习和练习SQL题目,不仅可以加深对数据库的理解,也有助于在面试中脱颖而出。

Global site tag (gtag.js) - Google Analytics