码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SQL基础知识(一)


    SQL基础知识(一)


    文章目录

    • 一、基础查询
      • 1.查询所有列
      • 2.查询多列
    • 二、简单处理查询结果
      • 1.查询结果去重
      • 2.查询结果限制返回行数
      • 3.将查询后的列重新命名
    • 三、基础操作符
      • 1.查找学校是北大的学生信息
      • 2.查找年龄大于24岁的用户信息
      • 3.查找某个年龄段的用户信息
      • 4.查找除复旦大学的用户信息
      • 5.用where过滤空值练习


    一、基础查询

    数据库基础数据:
    表名:user_profile

    iddevice_idgenderageuniversityprovince
    12138male21北京大学Beijing
    23214male复旦大学Shanghai
    36543female20北京大学Beijing
    42315female23浙江大学ZheJiang
    55432male25山东大学Shandong

    建表语句:

    CREATE TABLE `user_profile` (
    `id` int NOT NULL,
    `device_id` int NOT NULL,
    `gender` varchar(14) NOT NULL,
    `age` int ,
    `university` varchar(32) NOT NULL,
    `province` varchar(32)  NOT NULL);
    INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');
    INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
    INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
    INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
    INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1.查询所有列

    题目:现在运营想要查看用户信息表中所有的数据,请你取出相应结果

    select
      *
    from
      user_profile;
    
    • 1
    • 2
    • 3
    • 4

    2.查询多列

    题目:现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据

    select
      device_id,
      gender,
      age,
      university
    from
      user_profile;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    二、简单处理查询结果

    1.查询结果去重

    题目:现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。

    select
      distinct university
    from
      user_profile;
    
    • 1
    • 2
    • 3
    • 4

    2.查询结果限制返回行数

    题目:现在运营只需要查看前2个用户明细设备ID数据,请你从用户信息表 user_profile 中取出相应结果。

    select
      device_id
    from
      user_profile
    limit
      2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    STM32仿真器---V9资料包含软件驱动和使用说明-综合文档
    rar 星 36.05MB
    下载

    3.将查询后的列重新命名

    题目:现在你需要查看前2个用户明细设备ID数据,并将列名改为 ‘user_infos_example’,,请你从用户信息表取出相应结果。

    select
      device_id as user_infos_example
    from
      user_profile
    limit
      2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    三、基础操作符

    1.查找学校是北大的学生信息

    题目:现在运营想要筛选出所有北京大学的学生进行用户调研,请你从用户信息表中取出满足条件的数据,结果返回设备id和学校。

    select
      device_id,
      university
    from
      user_profile
    where
      university = "北京大学";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.查找年龄大于24岁的用户信息

    题目:现在运营想要针对24岁以上的用户开展分析,请你取出满足条件的设备ID、性别、年龄、学校。

    select
      device_id,
      gender,
      age,
      university
    from
      user_profile
    where
      age > 24;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.查找某个年龄段的用户信息

    题目:现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。

    SOL基础知识点总结
    docx 0星 超过10%的资源 826KB
    下载
    select
      device_id,
      gender,
      age
    from
      user_profile
    where
      age between 20 and 23;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    select
      device_id,
      gender,
      age
    from
      user_profile
    where
      age >= 20
      and age <= 23;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.查找除复旦大学的用户信息

    题目:现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据

    select
      device_id,
      gender,
      age,
      university
    from
      user_profile
    where
      university <> "复旦大学";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    select
      device_id,
      gender,
      age,
      university
    from
      user_profile
    where
      university != "复旦大学";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    select
      device_id,
      gender,
      age,
      university
    from
      user_profile
    where
      university not in ("复旦大学");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5.用where过滤空值练习

    题目:现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。

    select
      device_id,
      gender,
      age,
      university
    from
      user_profile
    where
      age is not null;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    随手点赞一次,运气增加一份。

  • 相关阅读:
    Python小白之PyCharm仍然显示“No module named ‘xlwings‘”
    HTML5新特性 day_03(8.9)Canvas的路径、动画,Three.js和地理位置
    go 通道的运用
    怎么利用邮件开发客户?
    npm常用命令详解
    如何在 qmake(QtCreator)中指定 Mac 平台
    最优控制理论(一)基本概念
    10-11 周三 shell xargs tr curl 做大事情
    【遍历二叉树算法描述】
    yolo v5 与 yolo v7 在一个项目中混合使用是否可行?
  • 原文地址:https://blog.csdn.net/tester_Xu_/article/details/126380153
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号