hpv阳性是什么意思| 梗塞灶是什么意思| 63年的兔是什么命| 补肾吃什么药最好| 金银花什么时候采摘最好| 肝气郁结西医叫什么病| 冰箱灯不亮是什么原因| 胃窦充血水肿意味着什么| 榴莲有什么作用| 抗病毒什么药效果好| 长期喝山楂水有什么好处和坏处| 孕妇可以用什么护肤品| 吃什么能流产| 33代表什么意思| 责成是什么意思| 7一9点是什么时辰| 枕芯用什么填充物好| 善莫大焉什么意思| 财神爷供奉什么供品| 花千骨什么时候上映的| 红色加蓝色等于什么颜色| 手掌心痒是什么原因| 牙齿有黑洞是什么原因| 腹膜刺激征是指什么| 唇炎是什么原因造成的| 曾是什么意思| 月经期喝红糖水有什么好处| 唉声叹气是什么意思| 胃肠炎吃什么药| 额头爱出汗是什么原因| 农历10月份是什么星座| 裸官是什么意思| 直肠壁增厚一般是什么情况| 气短挂什么科| 挚友什么意思| 怀孕了为什么还会出血| 验血能查出什么病| 晴空万里什么意思| 4个火读什么| 什么叫桑拿| 13年是什么年| 健忘是什么意思| 为什么会得腱鞘炎| 12月3号是什么星座| 越吃越瘦是什么原因| 备孕需要补充什么| 药物流产后吃什么好| 什么叫增值税| 马蹄粉是什么粉| 双鱼座什么性格| 未土是什么土| 什么泡水喝可以降血糖| 耄耋之年是什么意思| 乳腺无回声结节是什么意思| 肌底液是干什么用的| 相得益彰什么意思| 姑娘是什么意思| 脾门区结节是什么意思| 冠冕堂皇什么意思| 什么是关税| 什么奶茶最好喝| yair是什么牌子的空调| 云南是什么民族| 29周岁属什么生肖| 老舍原名叫什么| 男人遗精是什么原因| 胎儿左侧侧脑室增宽的原因是什么| 胰腺炎是什么病严重吗| npc是什么意思啊| 晚上9点半是什么时辰| 呼吸道感染吃什么药| 96年的鼠是什么命| 吃中药为什么要忌口| 老年痴呆症又叫什么名字| 黑色签字笔是什么笔| 贝五行属什么| 纸是什么生肖| 水丸是什么意思| 省委组织部长是什么级别| 灰色配什么颜色| 白内障有什么症状表现| 血液净化是什么意思| 狗狗打疫苗前后要注意什么| 来例假腰疼是什么原因| 比卡丘什么意思| 经常自言自语是什么原因| 心脏彩超挂什么科| 一什么扇子| 闫学晶是什么军衔| 盆腔炎做什么检查能查出来| 稽留流产是什么意思| 重症肌无力用什么药| 缠绵是什么意思| 女人吃什么越来越年轻| 熊喜欢吃什么食物| 吃什么会死| 龙女是什么意思| 胳膊肘往外拐是什么意思| 蒋介石为什么不杀张学良| 吃什么排毒最快| 金银花长什么样子图片| 6月26号是什么日子| 儿童c反应蛋白高说明什么| 广西属于什么地区| 手掌红什么原因| 结婚16年是什么婚| mu是什么单位| 发量多的女生适合什么发型| 微量元素6项是查什么| 75年的兔是什么命| 5月15日什么星座| 斑鸠吃什么食物| 粥样动脉硬化吃什么药| 为什么有的| aed是什么| 女性阳性是什么病| 什么时机塞给医生红包| 月经前几天是什么期| 普贤菩萨保佑什么生肖| 牙齿上白色斑块是什么| 国资委主任是什么级别| 1927年属什么生肖| 为什么qq| 偶尔胸闷是什么原因| 肉刺长什么样子图片| 霍山石斛有什么功效| 尿道炎和阴道炎有什么区别| 一九六八年属什么生肖| rds是什么意思| miki是什么意思| 我流是什么意思| 炎症吃什么药| 吃什么丰胸效果好又快| gummy是什么意思| 三个火念什么| 赭是什么颜色| 农历六月是什么夏| 什么是撸管| 5d电影是什么| 喝鲜牛奶有什么好处和坏处| 为什么膝盖弯曲就疼痛| normal是什么意思| 热痱子是什么原因引起的| 血糖低有什么症状| 为什么经常长口腔溃疡| 追溯码是什么意思| 慢阻肺吃什么药| 内痔疮吃什么药好得快| 执子之手与子偕老是什么意思| 尖锐湿疣什么症状| 耳朵前面有痣代表什么| 蚊子不咬什么体质的人| 为什么会低血压| 猫的五行属什么| 首脑是什么意思| 冠周炎吃什么药| 梦见老人死了又活了是什么意思| 手掌发麻是什么原因| 眼睛长麦粒肿用什么药| 香肉是什么肉| 外阴瘙痒用什么洗| 邪火是什么意思| kpi是什么意思啊| 1935年属什么| 自慰用什么| 老年人打嗝不止是什么原因| 路上遇到蛇是什么征兆| 晓五行属性是什么| 一直想吐是什么原因| 支那是什么意思| 万言万当不如一默是什么意思| 眉什么目什么| 口舌麻木吃什么药| 谶语是什么意思| 一个金字旁一个先读什么| 什么叫湿热| 离婚需要什么手续| 抑郁是什么意思| 海底轮是什么意思| 花蛤不能和什么一起吃| 红鸾星动是什么意思| 经常打嗝是什么原因引起的| 成吉思汗和忽必烈是什么关系| 发腮是什么意思| 母亲生日送什么礼物| 人绒毛膜促性腺激素是查什么的| 唇炎看什么科最好| 脉搏弱是什么原因| 智齿冠周炎吃什么消炎药| 什么牌子的裤子质量好| 便秘吃什么菜| 生理需求是什么意思| 接骨木莓是什么| 一什么而入| 轻浮是什么意思| 啤酒不能和什么一起吃| 2月9日什么星座| 经常流鼻涕是什么原因| 为什么叫香港脚| 低密度脂蛋白是什么| 浓绿的什么| 为什么合欢树又叫鬼树| 海绵肾是什么意思| 总胆红素是什么| 男孩取什么名字好听又有贵气| 断袖是什么意思| 嗓子痒干咳是什么原因| 火麻是什么植物| 手心为什么老出汗| 脑卒中是什么意思| 熬夜流鼻血是什么原因| 2039年是什么年| 杨八妹属什么生肖| 结节性甲状腺肿是什么意思| 经常跑步对身体有什么好处| 沙发客是什么意思| 甘露是什么| 肩周炎用什么药| 什么菜好消化又养胃| 右小指麻木是什么征兆| 脸颊两侧长痘痘什么原因| 舌头边疼是什么原因| 鹤膝风是什么病| 饕餮长什么样| 牙髓炎吃什么药最有效| 寿眉属于什么茶| 影子代表什么生肖| 体内湿热吃什么中成药| 膳食纤维是什么| 失焦是什么意思| 什么鱼吃玉米| kodice是什么牌子| 舌头发黄是什么问题| 手经常出汗是什么原因| 生育保险有什么用| 置之不理的置是什么意思| 庞统为什么叫凤雏| 爱因斯坦是什么学家| 91年出生的属什么| 知柏地黄丸有什么功效| 维字五行属什么| 支原体抗体阳性是什么意思| 软助什么意思| 健康证都查什么| 霏是什么意思| 息肉样病变是什么意思| 诺欣妥是什么药| 脑控是什么| 清新的什么填空| 油面筋是什么做的| 常见的贫血一般是缺什么| 包皮有什么影响| 气血不足吃什么药最好| 脚肿了是什么原因引起的| 阎王是什么意思| 阿尼是什么意思| 一路走好是什么意思| mva是什么单位| 豆浆和豆奶有什么区别| 寻找什么| 41年属什么生肖| strange什么意思| 366是什么意思| 大三阳转小三阳意味着什么| 百度
 

元气少年黄星羱牵手小芈月刘楚恬亮相北京电影节

百度 好不容易飞了10几个小时,来到了北欧,怎么能不玩个够本儿再走。

SQL is the essential language for developers, engineers, and data professionals. Intermediate knowledge in SQL gives you an edge in your data science career.



4 Useful Intermediate SQL Queries for Data Science
Photo by?Shubham Dhage?on?Unsplash

 

So in this post, we will discuss some of the essential intermediate SQL queries for data professionals. We will discuss 4 SQL queries, and the question is, why learn these 4 SQL queries?

We all are familiar with making a database in?SQL,?but more important is knowing how to clean and filter data effectively according to our needs. That's how these queries help us, so let's see what these queries are

  1. Trigger in SQL,?and how to use it in our query?
  2. Partition By?in SQL
  3. Limit in SQL,?and how do we use?LIMIT?syntax to limit our query in a SQL table?
  4. COALESCE function?in the SQL, and how it helps us to get rid of?NULL?values?

 

1. Trigger in SQL

 

4 Useful Intermediate SQL Queries for Data Science
TRIGGER in SQL | Source: Image by author

 

In this post, we will talk about the power of?triggers in SQL.

What is a Trigger in SQL?

The trigger is a SQL code that runs automatically when an event is performed. for example: In the below query, our trigger runs when the user tries to Insert values in the table.

The trigger is mainly divided into three parts

1. Trigger_Time

2. Trigger_Event

3. Table_name

1. Trigger_Time:?Trigger time means when you want this trigger to run, for example, before or after an event.

In our below query, we used before why? Because we want our code to run before the code is inserted into our table.

2. Trigger_Event:?The trigger event is when we want to run this trigger, such as?INSERT,?UPDATE, and?DELETE

In the above example, we use INSERT because we want to run our trigger when the INSERT event is performed.

3. Table_name:?table name is the name of our table

So let's discuss the trigger syntax in detail with a proper practical explanation

of the code between?BEGIN?and?END?is whenever we insert a value in the Voter table, the trigger checks the age, whether it is less than 18 or greater than 18. Nothing happens if it is greater than or equal to 18, but if it is less than 18, the trigger will show an error, and we set the message.

For example, see the above image,?we set message_text = ' age must be >=18',?so whenever we try to?insert the value less than 18, this error message shows. In the output section, when we try to insert Andrew, 17, the query shows an error, which contains our?custom message. So that's what triggers in SQL and how to use them in your query.

Check the below code for a more practical understanding

DELIMITER $$
/* creating a trigger whose name is age_checker */
  CREATE TRIGGER age_checker
     /* defining when our trigger run before inserting or after inserting
        in our case we choose before inserting that's why we use BEFORE */
      BEFORE INSERT ON Voter FOR EACH ROW
      BEGIN 
     /* Now we are checking the inserted value in our age column if it is 
        less then 18 then show our error message */ 
        IF NEW.age < 18
        THEN 
           SIGNAL SQLSTATE '45000'
              /* setting our error message in the MESSAGE_TEXT */
              SET MESSAGE_TEXT = 'AGE MUST BE >= 18';
        END IF;
     END;
DELIMETER;

 

2. Partition by in SQL

 

4 Useful Intermediate SQL Queries for Data Science
PARTITION BY in SQL | Source: Image by author

 

This post will discuss?partition?and how to use it in SQL.

So the first question is, what is a?partition in SQL?

The work of a partition is to group the rows with similar values but without limiting the rows.

Let's take an example for more clarity.

Please see the second image above.

There are five rows in the table in which there is a column named gender with three male candidates and two female candidates, so what if we want a new column that shows the number of males and females?

The partition syntax group the rows according to a similar value, which means that in our query, we use partition by gender. It means that we want to group our rows according to gender, so there are only two unique values in the gender column first is M and the second is F, so it divides the rows into two groups.

1 The rows which contain M in the gender column

2 The rows have F in the gender column, so that's the work of partition in SQL, and we use the count function to count several rows in the group.

In the output, you can see that the new column is built whose name is no_of_male_female, and this column contains the number of rows in the group. For example, there are two rows in the F group, so it shows 2, and there are three rows in the M group, so it shows 3.

Some might wonder

what the difference is between group by and partition by in SQL because both works are similar. So the answer is that if you use a group, your five rows convert only into two rows. Why?

Because the group by shows the number of groups, in our above query, our table is divided into two groups, M and F, so the group by only shows the first row of every group, which means that if the group contains ten rows but it shows only 1 row?in the output because it shows 1 row of every group.

But if you use partition by then, your number of rows remains unchanged as you see the number of rows in the output and the number of rows in the table remains the same.

Partition in SQL shows all rows.

Both have advantages and disadvantages you can use them according to your requirements.

Check the below code if you need it

/* count function used to count number of gender after partition and then 
   show output in no_of_male_female column */

SELECT * , COUNT(gender) OVER (PARTITION BY gender) 
AS no_of_male_female FROM EMPLOYEE;

 

3. Limit query

 

4 Useful Intermediate SQL Queries for Data Science
LIMIT in SQL | Source: Image by author

 

LIMIT in SQL and How it works, although LIMIT is not supported in some SQL databases, such as SQL servers and MS access.

LIMIT clause is widely used in SQL databases and primarily to limit the row.

For example: If we want to find the top 10 or worst ten albums from the database, then the limit is beneficial. We use LIMIT 10 in the last of the query, and our work is done.

But here is a question how does LIMIT work, and how can we use it effectively?

We can use LIMIT in two types

1. LIMIT ( any constant value )

For example, the below code selects the first eight rows from the table

SELECT * FROM table_name LIMIT 8;

2. LIMIT(x, y)

It's a more precise way to limit our tables.

The x argument is used to eliminate several rows from the top.

The y argument shows the number of rows you want after elimination.

For example:

Select * FROM table_name LIMIT 3,2;

this query eliminates three rows from the top and then shows the two rows after eliminating the three rows.

Check the below code.

/* This command is used to select first two rows */

SELECT * FROM EMPLOYEE LIMIT 2;


/* This command is used to select 3 rows after eliminating first two rows */

SELECT * FROM EMPLOYEE LIMIT 2,3;

 

4. COALESCE function

 

4 Useful Intermediate SQL Queries for Data Science
COALESCE function in SQL | Source: Image by author

 

Do you know that there are some great functions in SQL which save you time?

Here I am talking about the?COALESCE function,?and this function amazes you, so please read the full post.

The COALESCE function takes n number of values.

COALESCE(value_1,value_2,......, value_n)

 

So let's understand this; please see the above 4th image. You see that there is an EMPLOYEE table that contains a column whose name is located, and in that column, there are 3 NULL values. So what if we want to replace that NULL value with a meaningful word? So as in the above image, we want to replace all the NULL values with the word Missing_value.

We can do that with COALESCE function simply by seeing the code in the above image. The COALESCE function replaces all the NULL values with the word Missing_value.

Let's understand COALESCE function step by step.

Step 1:?The first argument we pass in the COALESCE function is the column name in which we want to find NULL values. That's why we use location.

Step 2: If it found some values, then nothing happens; as you will see in the output of the code in the image, the value remains the same, but if it found NULL values, then what it does that it simply replaces that NULL values with the word we give as the second argument in COALESCE function and we gave Missing_value as a second argument, and that's why it replaces all NULL value present in the location column with Missing_value.

So that's how to COALESCE function work in SQL. We were given the example of using a single column. We also do the same thing if we want to replace NULL values in multiple columns; we can also do that by following the above syntax.

Check the below code.

/* So we are targeting the location column from our EMPLOYEE table and 
   then replacing the NULL values with Missing_value text */
SELECT COALESCE(location,"Missing_value") as Location
FROM EMPLOYEE;

 

I hope this post will briefly explain how you use these essential queries in SQL and what are the benefits of using it. If you have any questions, feel free to ask in the comment section.

 
 
Ashbab Khan is a data scientist, SQL expert, and creative tech writer.

 
Original. Reposted with permission.
 



衤字旁与什么有关 桑黄长在什么树上 碳酸钙d3颗粒什么时候吃最好 一箭双雕是指什么生肖 扁桃体溃疡吃什么药
胃不好吃什么蔬菜 怀孕一周有什么症状 一月二十三号是什么星座 自变量是什么 吐血是什么原因引起的
威士忌是什么酿造的 化疗有什么副作用 10月10号是什么日子 隐疾是什么意思 公章是什么样的
管教有方是什么意思 绯闻是什么意思 焦俊艳和焦恩俊是什么关系 绘本是什么 血色素是什么
兰花什么时候开hcv9jop2ns4r.cn 体温低是什么原因hcv8jop2ns3r.cn 啤酒有什么牌子hcv8jop7ns3r.cn 西瓜为什么叫西瓜hcv9jop0ns9r.cn 笑靥什么意思hcv9jop6ns2r.cn
嫩牛五方什么意思youbangsi.com 人流后需要注意什么hcv8jop3ns9r.cn 吃什么能降铁蛋白bjcbxg.com 阑尾炎能吃什么hcv8jop3ns2r.cn 梦见发工资了是什么意思hcv7jop5ns0r.cn
女生适合抽什么烟hcv7jop9ns0r.cn 咳嗽白痰吃什么药hcv8jop2ns0r.cn 什么是音节什么是音序hcv8jop2ns8r.cn 契机是什么意思cj623037.com 华盖是什么意思96micro.com
吃紫甘蓝有什么好处hcv9jop0ns6r.cn 距离感是什么意思hcv8jop0ns6r.cn 坪效是什么意思hcv9jop1ns7r.cn 两个菱形是什么牌子bysq.com 青色五行属什么hcv8jop2ns6r.cn
百度