天天看點

SparkSQL綜合性練習

持之以恒,貴在堅持,每天進步一點點!

        之前推出過一期關于Spark的練習(👉​​Spark基礎能力自測題​​),反響還不錯。為了鞏固大家的基礎,提升實戰的能力,本期筆者又備下了一道關于SparkSQL,綜合性比較全面的基礎訓練題,希望大家能夠受用。

SparkSQL綜合性練習

準備資料

SparkSQL綜合性練習

student.txt

字段從左到右依次代表:

學号   學生姓名   學生性别  學生出生年月  學生所在班級      
108 丘東 男 1977-09-01 95033
105 匡明 男 1975-10-02 95031
107 王麗 女 1976-01-23 95033
101 李軍 男 1976-02-20 95033
109 王芳 女 1975-02-10 95031
103 陸君 男 1974-06-03 95031      

course.txt

課程号   課程名稱   教工編号      
3-105 計算機導論 825
3-245 作業系統 804
6-166 數字電路 856
9-888 高等數學 831      

score.txt

學号   課程号   成績      
103 3-245 86
105 3-245 75
109 3-245 68
103 3-105 92
105 3-105 88
109 3-105 76
101 3-105 64
107 3-105 91
108 3-105 78
101 6-166 85
107 6-166 79
108 6-166 81      

teacher.txt

教工編号   教工姓名  教工性别   教工出生年月  職稱    教工所在部門      
804 李誠 男 1958-12-02 副教授 計算機系
856 張旭 男 1969-03-12 講師 電子工程系
825 王萍 女 1972-05-05 助教 計算機系
831 劉冰 女 1977-08-14 助教 電子工程系      

上題

SparkSQL綜合性練習

1.查詢Student表中“95031”班或性别為“女”的同學記錄。

2.以Class降序,升序查詢Student表的所有記錄。

3.以Cno升序、Degree降序查詢Score表的所有記錄。

4.查詢“95031”班的學生。

5.查詢Score表中的最高分的學生學号和課程号。(子查詢或者排序)

6.查詢每門課的平均成績。

7.查詢Score表中至少有5名學生選修的并以3開頭的課程的平均分數。

8.查詢分數大于70,小于90的Sno列。

9.查詢所有學生的Sname、Cno和Degree列。

10.查詢所有學生的Sno、Cname和Degree列。

11.查詢所有學生的Sname、Cname和Degree列。

12.查詢“95033”班學生的平均分。

13.查詢所有選修“計算機導論”課程的“女”同學的成績表。

14.查詢選修“3-105”課程的成績高于“109”号同學成績的所有同學的記錄。

15.查詢score中選學多門課程的同學中分數為非最高分成績的記錄。

16.查詢成績高于學号為“109”、課程号為“3-105”的成績的所有記錄。

17.查詢和學号為105的同學同年出生的所有學生的Sno、Sname和Sbirthday列。

18.查詢“張旭“教師任課的學生成績

19.查詢選修某課程的同學人數多于4人的教師姓名

20.查詢95033班和95031班全體學生的記錄

21.查詢存在有85分以上成績的課程Cno

22.查詢出“計算機系“教師所教課程的成績表

23.查詢“計算機系”與“電子工程系“不同職稱的教師的Tname和Prof

24.查詢選修編号為“3-105“課程且成績至少高于選修編号為“3-245”的同學的Cno、Sno和Degree,并按Degree從高到低次序排序

25.查詢選修編号為“3-105”且成績高于選修編号為“3-245”課程的同學的Cno、Sno和Degree

26.查詢所有教師和同學的name、sex和birthday

27.查詢所有“女”教師和“女”同學的name、sex和birthday.

28.查詢成績比該課程平均成績低的同學的成績表

29.查詢所有任課教師的Tname和Depart

30.查詢所有未講課的教師的Tname和Depart

31.查詢至少有2名男生的班号

32.查詢Student表中不姓“王”的同學記錄

33.查詢Student表中每個學生的姓名和年齡。

34.查詢Student表中最大和最小的Sbirthday日期值。(時間格式最大值,最小值)

35.以班号和年齡從大到小的順序查詢Student表中的全部記錄。查詢結果排序

36.查詢“男”教師及其所上的課程

37.查詢最高分同學的Sno、Cno和Degree列

38.查詢和“李軍”同性别的所有同學的Sname

39.查詢和“李軍”同性别并同班的同學Sname

40.查詢所有選修“計算機導論”課程的“男”同學的成績表

41.查詢Student表中的所有記錄的Sname、Ssex和Class列

42.查詢教師所有的機關即不重複的Depart列

43.查詢Student表的所有記錄

44.查詢Score表中成績在60到80之間的所有記錄

45.查詢Score表中成績為85,86或88的記錄

答案

聲明:下面的答案均為部落客自己的解法,結果均經得起測試,如有纰漏,煩請大佬指點。

SparkSQL綜合性練習
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{Dataset, SparkSession}

/*
 * @Auther: Alice菌
 * @Date: 2020/4/18 08:14
 * @Description: 
    流年笑擲 未來可期。以夢為馬,不負韶華!
 */
object demo06 {

  // 定義樣例類封裝資料
  case class Student(Sno:String,Sname:String,Ssex:String,Sbirthday:String,Class:String)
  case class Course(Cno:String,Cname:String,Tno:String)
  case class Score(Sno:String,Cno:String,Degree:String)
  case class Teacher(Tno:String,Tname:String,Tsex:String,Tbirthday:String,Prof:String,Depart:String)


  def main(args: Array[String]): Unit = {


    // 1. 建立SparkSession
    val spark: SparkSession = SparkSession.builder().master("local[*]").appName("demo06").getOrCreate()

    val sc: SparkContext = spark.sparkContext

    // 設定日志級别
    sc.setLogLevel("WARN")

    // 讀取test檔案
   val rdd1: RDD[String] = sc.textFile("E:\\2020大資料新學年\\BigData\\05-Spark\\0417\\4.16号練習題50道2.0\\data\\student.txt")
   val rdd2: RDD[String] = sc.textFile("E:\\2020大資料新學年\\BigData\\05-Spark\\0417\\4.16号練習題50道2.0\\data\\course.txt")
   val rdd3: RDD[String] = sc.textFile("E:\\2020大資料新學年\\BigData\\05-Spark\\0417\\4.16号練習題50道2.0\\data\\score.txt")
   val rdd4: RDD[String] = sc.textFile("E:\\2020大資料新學年\\BigData\\05-Spark\\0417\\4.16号練習題50道2.0\\data\\teacher.txt")

    val student: RDD[Student] = rdd1.map(x=>{val str = x.split("\t");Student(str(0),str(1),str(2),str(3),str(4))})
    val course: RDD[Course] = rdd2.map(x=>{val str = x.split("\t");Course(str(0),str(1),str(2))})
    val score: RDD[Score] = rdd3.map(x=>{val str = x.split("\t");Score(str(0),str(1),str(2))})
    val teacher: RDD[Teacher] = rdd4.map(x=>{val str = x.split("\t");Teacher(str(0),str(1),str(2),str(3),str(4),str(5))})

    // 導入隐式轉換
    import spark.implicits._

    // 将RDD轉換成DF
    val stuframe = student.toDF()
    val couframe = course.toDF()
    val scoframe = score.toDF()
    val teaframe = teacher.toDF()

    // 建立臨時表
    stuframe.createOrReplaceTempView("student")
    couframe.createOrReplaceTempView("course")
    scoframe.createOrReplaceTempView("score")
    teaframe.createOrReplaceTempView("teacher")


    /* 6. 查詢Student表中“95031”班或性别為“女”的同學記錄*/
    spark.sql("Select * from student where sno = '95031' or ssex = '女'").show()
    //+---+-----+----+----------+-----+
    //|Sno|Sname|Ssex| Sbirthday|Class|
    //+---+-----+----+----------+-----+
    //|107|   王麗|   女|1976-01-23|95033|
    //|109|   王芳|   女|1975-02-10|95031|
    //+---+-----+----+----------+-----+

    /* 7. 以Class降序,升序查詢Student表的所有記錄 */
    spark.sql("Select * from student order by class desc").show()
    //+---+-----+----+----------+-----+
    //|Sno|Sname|Ssex| Sbirthday|Class|
    //+---+-----+----+----------+-----+
    //|107|   王麗|   女|1976-01-23|95033|
    //|101|   李軍|   男|1976-02-20|95033|
    //|108|   丘東|   男|1977-09-01|95033|
    //|105|   匡明|   男|1975-10-02|95031|
    //|109|   王芳|   女|1975-02-10|95031|
    //|103|   陸君|   男|1974-06-03|95031|
    //+---+-----+----+----------+-----+

    /* 8. 以Cno升序、Degree降序查詢Score表的所有記錄。 */
    spark.sql("Select * from score order by cno,degree desc").show()
    //+---+-----+------+
    //|Sno|  Cno|Degree|
    //+---+-----+------+
    //|103|3-105|    92|
    //|107|3-105|    91|
    //|105|3-105|    88|
    //|108|3-105|    78|
    //|109|3-105|    76|
    //|101|3-105|    64|
    //|103|3-245|    86|
    //|105|3-245|    75|
    //|109|3-245|    68|
    //|101|6-166|    85|
    //|108|6-166|    81|
    //|107|6-166|    79|
    //+---+-----+------+

    /* 9. 查詢“95031”班的學生。 */
    spark.sql("Select sname from student where class = '95031'").show()
    //+-----+
    //|sname|
    //+-----+
    //|   匡明|
    //|   王芳|
    //|   陸君|
    //+-----+

    /* 10. 查詢Score表中的最高分的學生學号和課程号。(子查詢或者排序) */
    spark.sql("Select sno,cno from score order by degree desc limit 1").show()
    //+---+-----+
    //|sno|  cno|
    //+---+-----+
    //|103|3-105|
    //+---+-----+

    /* 11. 查詢每門課的平均成績。  */
    spark.sql("Select first(course.Cname),avg(degree) from score,course where score.Cno = course.Cno  group by score.Cno").show()
    //+-------------------+---------------------------+
    //|first(Cname, false)|avg(CAST(degree AS DOUBLE))|
    //+-------------------+---------------------------+
    //|               數字電路|          81.66666666666667|
    //|               作業系統|          76.33333333333333|
    //|              計算機導論|                       81.5|
    //+-------------------+---------------------------+

    /* 12. 查詢Score表中至少有5名學生選修的并以3開頭的課程的平均分數。  */
    spark.sql("Select first(cno),avg(Degree) from score where cno like '3%' group by cno having count(cno) >= 5").show()
    //+-----------------+---------------------------+
    //|first(cno, false)|avg(CAST(Degree AS DOUBLE))|
    //+-----------------+---------------------------+
    //|            3-105|                       81.5|
    //+-----------------+---------------------------+

    /* 13.查詢分數大于70,小于90的Sno列  */
    spark.sql("Select sno from score where Degree > 70 and Degree < 90 ").show()
    //+---+
    //|sno|
    //+---+
    //|103|
    //|105|
    //|105|
    //|109|
    //|108|
    //|101|
    //|107|
    //|108|
    //+---+

    /* 14.查詢所有學生的Sname、Cno和Degree列。 */
    spark.sql("select sname,cno,degree from score,student where score.sno = student.sno").show()
    //+-----+-----+------+
    //|sname|  cno|degree|
    //+-----+-----+------+
    //|   李軍|3-105|    64|
    //|   李軍|6-166|    85|
    //|   王麗|3-105|    91|
    //|   王麗|6-166|    79|
    //|   陸君|3-245|    86|
    //|   陸君|3-105|    92|
    //|   丘東|3-105|    78|
    //|   丘東|6-166|    81|
    //|   匡明|3-245|    75|
    //|   匡明|3-105|    88|
    //|   王芳|3-245|    68|
    //|   王芳|3-105|    76|
    //+-----+-----+------+

    /* 15.查詢所有學生的Sno、Cname和Degree列。 */
    spark.sql("select sno,cname,degree from score,course where score.cno=course.cno").show()
    //+---+-----+------+
    //|sno|cname|degree|
    //+---+-----+------+
    //|101| 數字電路|    85|
    //|107| 數字電路|    79|
    //|108| 數字電路|    81|
    //|103| 作業系統|    86|
    //|105| 作業系統|    75|
    //|109| 作業系統|    68|
    //|103|計算機導論|    92|
    //|105|計算機導論|    88|
    //|109|計算機導論|    76|
    //|101|計算機導論|    64|
    //|107|計算機導論|    91|
    //|108|計算機導論|    78|
    //+---+-----+------+

    /* 16.查詢所有學生的Sname、Cname和Degree列。 */
    spark.sql(
      """
        |select sname,cname,degree from score,course,student
        |where score.cno = course.cno and student.sno = score.sno
      """.stripMargin).show()
     //+-----+-----+------+
    //|sname|cname|degree|
    //+-----+-----+------+
    //|   李軍| 數字電路|    85|
    //|   李軍|計算機導論|    64|
    //|   王麗| 數字電路|    79|
    //|   王麗|計算機導論|    91|
    //|   陸君| 作業系統|    86|
    //|   陸君|計算機導論|    92|
    //|   丘東| 數字電路|    81|
    //|   丘東|計算機導論|    78|
    //|   匡明| 作業系統|    75|
    //|   匡明|計算機導論|    88|
    //|   王芳| 作業系統|    68|
    //|   王芳|計算機導論|    76|
    //+-----+-----+------+

    /* 17.查詢“95033”班學生的平均分。*/
    spark.sql("select first(class),avg(Degree) from student,score where student.sno = score.sno and class = '95033'").show()
    //+-------------------+---------------------------+
    //|first(class, false)|avg(CAST(Degree AS DOUBLE))|
    //+-------------------+---------------------------+
    //|              95033|          79.66666666666667|
    //+-------------------+---------------------------+

    /* 18.查詢所有選修“計算機導論”課程的“女”同學的成績表。 */
    spark.sql(
      """
        |select sname,cname,ssex,Degree from student,course,score
        |where student.sno = score.sno and course.cno=score.cno
        |and ssex = "女" and cname = "計算機導論"
      """.stripMargin).show()
    //|sname|cname|ssex|Degree|
    //+-----+-----+----+------+
    //|   王麗|計算機導論|   女|    91|
    //|   王芳|計算機導論|   女|    76|
    //+-----+-----+----+------+

    /* 19.查詢選修“3-105”課程的成績高于“109”号同學成績的所有同學的記錄。*/
    spark.sql(
      """
        |select * from score where cno = "3-105" and degree > (select degree from score where sno = "109" and cno = "3-105" )
        |
      """.stripMargin).show()
    //+---+-----+------+
    //|Sno|  Cno|Degree|
    //+---+-----+------+
    //|103|3-105|    92|
    //|105|3-105|    88|
    //|107|3-105|    91|
    //|108|3-105|    78|
    //+---+-----+------+

    /* 20.查詢score中選學多門課程的同學中分數為非最高分成績的記錄。  */
//    spark.sql(
//      """
//        |select first(sno),first(cno),first(degree) from score a where sno in
//        |(select sno from score group by sno having count(*)>1)    
//        |and degree<(select max(degree) from score b where a.cno=b.cno)      
//      """.stripMargin).show()
    spark.sql("select * from score a where sno in (select sno from score group by sno having count(*)>1) and degree<(select max(degree) from score b where a.cno=b.cno )").show()
    //+---+-----+------+
    //|Sno|  Cno|Degree|
    //+---+-----+------+
    //|107|6-166|    79|
    //|108|6-166|    81|
    //|105|3-245|    75|
    //|109|3-245|    68|
    //|101|3-105|    64|
    //|107|3-105|    91|
    //|108|3-105|    78|
    //|105|3-105|    88|
    //|109|3-105|    76|
    //+---+-----+------+

    /* 21.查詢成績高于學号為“109”、課程号為“3-105”的成績的所有記錄。 */
     spark.sql("select * from score where degree > (select degree from score where sno = '109' and cno = '3-105')").show()
    //+---+-----+------+
    //|Sno|  Cno|Degree|
    //+---+-----+------+
    //|103|3-245|    86|
    //|103|3-105|    92|
    //|105|3-105|    88|
    //|107|3-105|    91|
    //|108|3-105|    78|
    //|101|6-166|    85|
    //|107|6-166|    79|
    //|108|6-166|    81|
    //+---+-----+------+

    /* 22. 查詢和學号為105的同學同年出生的所有學生的Sno、Sname和Sbirthday列。*/

    spark.udf.register("sub",(str:String,num1:Int,num2:Int)=>str.substring(num1,num2))

    spark.sql("select sno,sname,sbirthday from student where sub(Sbirthday,0,5) = (select sub(Sbirthday,0,5) from student where sno = '105') ").show()
    //+---+-----+----------+
    //|sno|sname| sbirthday|
    //+---+-----+----------+
    //|105|   匡明|1975-10-02|
    //|109|   王芳|1975-02-10|
    //+---+-----+----------+

    /* 23. 查詢“張旭“教師任課的學生成績 */
    spark.sql(
      """
        |select  sno,degree from score,course,teacher
        |where tname = "張旭" and teacher.tno = course.tno
        |and course.Cno = score.Cno
      """.stripMargin).show()
    //+---+------+
    //|sno|degree|
    //+---+------+
    //|101|    85|
    //|107|    79|
    //|108|    81|
    //+---+------+

    /* 24. 查詢選修某課程的同學人數多于4人的教師姓名。 */
     spark.sql(
       """
         |select first(tname) from teacher,score,course
         |where teacher.tno = course.tno
         |and course.Cno = score.Cno
         |group by score.Cno having count(score.Cno) > 4
       """.stripMargin).show()

    //+-------------------+
    //|first(tname, false)|
    //+-------------------+
    //|                 王萍|
    //+-------------------+

    /* 25. 查詢95033班和95031班全體學生的記錄。 */
     spark.sql("select * from student where class= '95033' or class = '95031'").show()
    //+---+-----+----+----------+-----+
    //|Sno|Sname|Ssex| Sbirthday|Class|
    //+---+-----+----+----------+-----+
    //|108|   丘東|   男|1977-09-01|95033|
    //|105|   匡明|   男|1975-10-02|95031|
    //|107|   王麗|   女|1976-01-23|95033|
    //|101|   李軍|   男|1976-02-20|95033|
    //|109|   王芳|   女|1975-02-10|95031|
    //|103|   陸君|   男|1974-06-03|95031|
    //+---+-----+----+----------+-----+

    /* 26. 查詢存在有85分以上成績的課程Cno.  */
     spark.sql("select distinct cno from score where degree >= 85").show()
    //+-----+
    //|  cno|
    //+-----+
    //|6-166|
    //|3-245|
    //|3-105|
    //+-----+

    /* 27. 查詢出“計算機系“教師所教課程的成績表。 */
     spark.sql(
       """
         |select  score.*  from score,Course,teacher
         |where  teacher.tno = course.tno
         |and course.Cno = score.Cno
         |and teacher.Depart = "計算機系"
       """.stripMargin).show()
    //+---+-----+------+
    //|Sno|  Cno|Degree|
    //+---+-----+------+
    //|103|3-105|    92|
    //|105|3-105|    88|
    //|109|3-105|    76|
    //|101|3-105|    64|
    //|107|3-105|    91|
    //|108|3-105|    78|
    //|103|3-245|    86|
    //|105|3-245|    75|
    //|109|3-245|    68|
    //+---+-----+------+

    /* 28. 查詢“計算機系”與“電子工程系“不同職稱的教師的Tname和Prof */
    spark.sql(
      """
        |select tname,prof from teacher where depart = '計算機系' and prof not in
        |(select prof from teacher where depart = '電子工程系')
      """.stripMargin).show()
    //+-----+----+
    //|tname|prof|
    //+-----+----+
    //|   李誠| 副教授|
    //+-----+----+

    /* 29. 查詢選修編号為“3-105“課程且成績至少高于選修編号為“3-245”的同學的Cno、Sno和Degree,并按Degree從高到低次序排序。 */
     spark.sql(
       """
         |select s1.cno,s1.sno,s1.degree from score s1,score s2
         |where s1.cno = "3-105" and s2.cno = "3-245"
         |and s1.Sno = s2.Sno
         |and s1.degree > s2. degree
         |order by s1.degree desc
       """.stripMargin).show()

    //+-----+---+------+
    //|  cno|sno|degree|
    //+-----+---+------+
    //|3-105|103|    92|
    //|3-105|105|    88|
    //|3-105|109|    76|
    //+-----+---+------+

    /* 30. 查詢選修編号為“3-105”且成績高于選修編号為“3-245”課程的同學的Cno、Sno和Degree. */
    spark.sql(
      """
        |select cno,sno,degree from score
        |where cno = "3-105" and degree > (select max(degree) from score where cno = "3-245")
      """.stripMargin).show()
    //+-----+---+------+
    //|  cno|sno|degree|
    //+-----+---+------+
    //|3-105|103|    92|
    //|3-105|105|    88|
    //|3-105|107|    91|
    //+-----+---+------+

    /* 31. 查詢所有教師和同學的name、sex和birthday. */
     spark.sql(
       """
         |select distinct Sname as name,Ssex as sex,Sbirthday as birthday from student
         |union
         |select distinct Tname as name,Tsex as sex,Tbirthday as birthday from Teacher
         |
       """.stripMargin).show()
    //+----+---+----------+
    //|name|sex|  birthday|
    //+----+---+----------+
    //|  李誠|  男|1958-12-02|
    //|  張旭|  男|1969-03-12|
    //|  王萍|  女|1972-05-05|
    //|  李軍|  男|1976-02-20|
    //|  匡明|  男|1975-10-02|
    //|  劉冰|  女|1977-08-14|
    //|  陸君|  男|1974-06-03|
    //|  王芳|  女|1975-02-10|
    //|  王麗|  女|1976-01-23|
    //|  丘東|  男|1977-09-01|
    //+----+---+----------+

    /* 32. 查詢所有“女”教師和“女”同學的name、sex和birthday. */
    spark.sql(
      """
        |select distinct Sname as name,Ssex as sex,Sbirthday as birthday from student where Ssex = "女"
        |union
        |select distinct Tname as name,Tsex as sex,Tbirthday as birthday from Teacher where Tsex = "女"
        |
      """.stripMargin).show()
    //+----+---+----------+
    //|name|sex|  birthday|
    //+----+---+----------+
    //|  王萍|  女|1972-05-05|
    //|  劉冰|  女|1977-08-14|
    //|  王芳|  女|1975-02-10|
    //|  王麗|  女|1976-01-23|
    //+----+---+----------+

    /* 33. 查詢成績比該課程平均成績低的同學的成績表。 */
     spark.sql(
       """
         |select * from score s1
         |where degree < (select avg(degree) from score s2 where s1.Cno = s2.Cno)
       """.stripMargin).show()
    //+---+-----+------+
    //|Sno|  Cno|Degree|
    //+---+-----+------+
    //|107|6-166|    79|
    //|108|6-166|    81|
    //|105|3-245|    75|
    //|109|3-245|    68|
    //|109|3-105|    76|
    //|101|3-105|    64|
    //|108|3-105|    78|
    //+---+-----+------+

    /* 34. 查詢所有任課教師的Tname和Depart. */
     spark.sql(
       """
         |select tname,depart from teacher
         |where tno in (select tno from course where cno in (select distinct cno from score) )
       """.stripMargin).show()
    //+-----+------+
    //|tname|depart|
    //+-----+------+
    //|   張旭| 電子工程系|
    //|   王萍|  計算機系|
    //|   李誠|  計算機系|
    //+-----+------+

    /* 35. 查詢所有未講課的教師的Tname和Depart.  */
   spark.sql(
     """
       |select tname,depart from teacher
       |where tno not in (select tno from course where cno in (select distinct cno from score) )
     """.stripMargin).show()
    //+-----+------+
    //|tname|depart|
    //+-----+------+
    //|   劉冰| 電子工程系|
    //+-----+------+

    /* 36. 查詢至少有2名男生的班号。 */
    spark.sql(
      """
        |select class from student
        |where ssex = "男"
        |group by class having count(class) >= 2
      """.stripMargin).show()
    //+-----+
    //|class|
    //+-----+
    //|95033|
    //|95031|
    //+-----+

    /* 37. 查詢Student表中不姓“王”的同學記錄。 */
     spark.sql(
       """
         |select * from student
         |where sname not like "王%"
       """.stripMargin).show()
    //+---+-----+----+----------+-----+
    //|Sno|Sname|Ssex| Sbirthday|Class|
    //+---+-----+----+----------+-----+
    //|108|   丘東|   男|1977-09-01|95033|
    //|105|   匡明|   男|1975-10-02|95031|
    //|101|   李軍|   男|1976-02-20|95033|
    //|103|   陸君|   男|1974-06-03|95031|
    //+---+-----+----+----------+-----+

    /* 38. 查詢Student表中每個學生的姓名和年齡。将函數運用到spark sql中去計算,可以直接拿String的類型計算不需要再轉換成數值型 預設是會轉換成Double類型計算浮點型轉整型 */

    spark.sql(
      """
        |select Sname,year(current_date)-year(Sbirthday) from student
      """.stripMargin).show()
    //+-----+------------------------------------------------------+
    //|Sname|(year(current_date()) - year(CAST(Sbirthday AS DATE)))|
    //+-----+------------------------------------------------------+
    //|   丘東|                                                    43|
    //|   匡明|                                                    45|
    //|   王麗|                                                    44|
    //|   李軍|                                                    44|
    //|   王芳|                                                    45|
    //|   陸君|                                                    46|
    //+-----+------------------------------------------------------+

    /* 39. 查詢Student表中最大和最小的Sbirthday日期值。 時間格式最大值,最小值*/
    spark.sql(
      """
        |select max(Sbirthday),min(Sbirthday) from student
      """.stripMargin).show()
    //+--------------+--------------+
    //|max(Sbirthday)|min(Sbirthday)|
    //+--------------+--------------+
    //|    1977-09-01|    1974-06-03|
    //+--------------+--------------+

    /* 40. 以班号和年齡從大到小的順序查詢Student表中的全部記錄。 查詢結果排序*/
    spark.sql(
      """
        |select * from student order by class desc,year(current_date)-year(Sbirthday) desc
      """.stripMargin).show()
    //+---+-----+----+----------+-----+
    //|Sno|Sname|Ssex| Sbirthday|Class|
    //+---+-----+----+----------+-----+
    //|107|   王麗|   女|1976-01-23|95033|
    //|101|   李軍|   男|1976-02-20|95033|
    //|108|   丘東|   男|1977-09-01|95033|
    //|103|   陸君|   男|1974-06-03|95031|
    //|105|   匡明|   男|1975-10-02|95031|
    //|109|   王芳|   女|1975-02-10|95031|
    //+---+-----+----+----------+-----+

    /* 41. 查詢“男”教師及其所上的課程。 */
    spark.sql(
      """
        |select cname from course,teacher
        |where teacher.Tno = course.Tno and Tsex = "男"
      """.stripMargin).show()
    //+-----+
    //|cname|
    //+-----+
    //| 數字電路|
    //| 作業系統|
    //+-----+

    /* 42. 查詢最高分同學的Sno、Cno和Degree列。  */
    spark.sql(
      """
        |select * from score
        |where degree = (select max(degree) from score)
      """.stripMargin).show()
    //+---+-----+------+
    //|Sno|  Cno|Degree|
    //+---+-----+------+
    //|103|3-105|    92|
    //+---+-----+------+

    /* 43. 查詢和“李軍”同性别的所有同學的Sname.*/
    spark.sql(
      """
        |select sname from student
        |where ssex = (select ssex from student where sname = "李軍") and sname != "李軍"
      """.stripMargin).show()
    //+-----+
    //|sname|
    //+-----+
    //|   丘東|
    //|   匡明|
    //|   李軍|
    //|   陸君|
    //+-----+

    /* 44. 查詢和“李軍”同性别并同班的同學Sname. */
    spark.sql(
      """
        |select distinct s1.sname from student s1,student s2
        |where s1.ssex = s2.ssex and s1.class = s2.class
        |and s1.sname != "李軍"
      """.stripMargin).show()
    //+-----+
    //|sname|
    //+-----+
    //|   陸君|
    //|   匡明|
    //|   王芳|
    //|   王麗|
    //|   丘東|
    //+-----+

    /* 45. 查詢所有選修“計算機導論”課程的“男”同學的成績表。 */
    spark.sql(
      """
        |select degree from score,student,course
        |where score.cno = course.cno
        |and score.sno = student.sno
        |and student.ssex = "男"
        |and course.cname = "計算機導論"
      """.stripMargin).show()
    //+------+
    //|degree|
    //+------+
    //|    64|
    //|    92|
    //|    78|
    //|    88|
    //+------+

    /* 46. 查詢Student表中的所有記錄的Sname、Ssex和Class列。 */
     spark.sql(
       """
         |select sname,ssex,class from student
       """.stripMargin).show()
    //+-----+----+-----+
    //|   丘東|   男|95033|
    //|   匡明|   男|95031|
    //|   王麗|   女|95033|
    //|   李軍|   男|95033|
    //|   王芳|   女|95031|
    //|   陸君|   男|95031|
    //+-----+----+-----+

    /* 47. 查詢教師所有的機關即不重複的Depart列。*/
     spark.sql(
       """
         |select distinct depart from teacher
       """.stripMargin).show()
    //+------+
    //|depart|
    //+------+
    //| 電子工程系|
    //|  計算機系|
    //+------+

    /* 48. 查詢Student表的所有記錄 */
    spark.sql(
      """
        |select * from student
      """.stripMargin).show()
    //+---+-----+----+----------+-----+
    //|Sno|Sname|Ssex| Sbirthday|Class|
    //+---+-----+----+----------+-----+
    //|108|   丘東|   男|1977-09-01|95033|
    //|105|   匡明|   男|1975-10-02|95031|
    //|107|   王麗|   女|1976-01-23|95033|
    //|101|   李軍|   男|1976-02-20|95033|
    //|109|   王芳|   女|1975-02-10|95031|
    //|103|   陸君|   男|1974-06-03|95031|
    //+---+-----+----+----------+-----+

    /* 49. 查詢Score表中成績在60到80之間的所有記錄。 */
    spark.sql(
      """
        |select * from score where degree > 60 and degree < 80
      """.stripMargin).show()
    //+---+-----+------+
    //|Sno|  Cno|Degree|
    //+---+-----+------+
    //|105|3-245|    75|
    //|109|3-245|    68|
    //|109|3-105|    76|
    //|101|3-105|    64|
    //|108|3-105|    78|
    //|107|6-166|    79|
    //+---+-----+------+

    /* 50. 查詢Score表中成績為85,86或88的記錄。 */
     spark.sql(
       """
         |select * from score where Degree in (85,86,88)
       """.stripMargin).show()
    //+---+-----+------+
    //|Sno|  Cno|Degree|
    //+---+-----+------+
    //|103|3-245|    86|
    //|105|3-105|    88|
    //|101|6-166|    85|
    //+---+-----+------+


  }
}      

結語

感謝您的閱讀,感覺作者寫的有點東西可以三連支援一下。你知道的越多,你不知道的也越多,我是Alice,我們下一期見!​

 受益的朋友記得三連支援小菌!

SparkSQL綜合性練習

繼續閱讀