SQL LEFT and RIGHT

本文介绍了SQL中的LEFT()和RIGHT()函数,用于从字符串中提取指定数量的字符。LEFT()函数从左开始提取,RIGHT()函数从右开始。在账户表中,通过这两个函数可以分析网站类型的分布,例如提取最后三位以确定网址类型。同时,通过分析公司名称首字母,发现约99.7%的公司名称以字母开头,而只有0.3%以数字开头。此外,还探讨了公司名称以元音字母开头的比例,发现22.8%的公司名称以元音开头,其余77.2%则不然。

Definition

  • The LEFT() function extracts a number of characters from a string (starting from left).

  • The RIGHT() function extracts a number of characters from a string (starting from right).

  1. In the accounts table, there is a column holding the website for each company. The last three digits specify what type of web address they are using. A list of extensions (and pricing) is provided here. Pull these extensions and provide how many of each website type exist in the accounts table.
SELECT RIGHT(website, 3) AS domain, COUNT(*) num_companies
FROM accounts
GROUP BY 1
ORDER BY 2 DESC;
  1. There is much debate about how much the name (or even the first letter of a company name) matters. Use the accounts table to pull the first letter of each company name to see the distribution of company names that begin with each letter (or number).
SELECT LEFT(UPPER(name), 1) AS first_letter, COUNT(*) num_companies
FROM accounts
GROUP BY 1
ORDER BY 2 DESC;
  1. Use the accounts table and a CASE statement to create two groups: one group of company names that start with a number and a second group of those company names that start with a letter. What proportion of company names start with a letter?
SELECT SUM(num) nums, SUM(letter) letters
FROM (SELECT name, CASE WHEN LEFT(UPPER(name), 1) IN ('0','1','2','3','4','5','6','7','8','9') 
                       THEN 1 ELSE 0 END AS num, 
         CASE WHEN LEFT(UPPER(name), 1) IN ('0','1','2','3','4','5','6','7','8','9') 
                       THEN 0 ELSE 1 END AS letter
      FROM accounts) t1;

There are 350 company names that start with a letter and 1 that starts with a number. This gives a ratio of 350/351 that are company names that start with a letter or 99.7%.

  1. Consider vowels as a, e, i, o, and u. What proportion of company names start with a vowel, and what percent start with anything else?
SELECT SUM(vowels) vowels, SUM(other) other
FROM (SELECT name, CASE WHEN LEFT(UPPER(name), 1) IN ('A','E','I','O','U') 
                        THEN 1 ELSE 0 END AS vowels, 
          CASE WHEN LEFT(UPPER(name), 1) IN ('A','E','I','O','U') 
                       THEN 0 ELSE 1 END AS other
         FROM accounts) t1;

There are 80 company names that start with a vowel and 271 that start with other characters. Therefore 80/351 are vowels or 22.8%. Therefore, 77.2% of company names do not start with vowels.

### SQL LEFT JOIN 使用示例 #### 示例语法解释 当执行 `LEFT JOIN` 操作时,会返回左表中的所有记录以及右表中满足连接条件的匹配记录。如果右表中不存在匹配,则结果集中对应的位置显示为 NULL。 ```sql SELECT * FROM table_left tl LEFT JOIN table_right tr ON tl.common_field = tr.common_field; ``` 此语句表示从 `table_left` 中选取所有的行,并尝试基于共同字段 `common_field` 进行关联。对于那些在 `table_right` 表中有相应匹配项的数据行,在最终的结果集里将会展示来自两张表格的信息;而对于找不到任何匹配的情况,则仅保留左侧数据并填充右侧为空值[^2]。 #### 实际应用案例 考虑学生(`student`)及其班级(`class`)的关系: ```sql SELECT * FROM student s LEFT JOIN class c ON s.c_num = c.id; ``` 上述命令旨在获取每一位学生的资料连同他们所属的具体班次详情。即使某些同学尚未分配到特定年级或课程内,这类情形下依旧会在输出列表里面体现该生基本信息的同时留置空白作为未定级处理状态的表现形式[^3]。 为了进一步理解复杂场景下的运用方式,这里给出一个更贴近实际业务逻辑的例子——假设有一个员工职称变更历史记录表 (`titles`) 和一张按个人编号汇总最新任职日期子查询临时表 `(et)`: ```sql SELECT t.emp_no, t.title FROM titles t LEFT JOIN ( SELECT emp_no, MAX(from_date) AS max_date FROM titles GROUP BY emp_no ) et ON t.emp_no = et.emp_no AND t.from_date = et.max_date; ``` 这段脚本的作用是从 `titles` 数据集合挑选每位雇员最新的头衔信息。通过内部嵌套查询先计算出各成员最后一次职务变动的确切时间戳记,再利用外部层面对比实现精准定位至单一条目上完成读取操作[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值