<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/category/sql/</link>
	<description></description>
	<lastBuildDate>Tue, 03 Apr 2018 15:18:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>SQL Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/category/sql/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 627. Swap Salary</title>
		<link>https://zxi.mytechroad.com/blog/sql/leetcode-627-swap-salary/</link>
					<comments>https://zxi.mytechroad.com/blog/sql/leetcode-627-swap-salary/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 03 Apr 2018 15:17:59 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[swap]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2416</guid>

					<description><![CDATA[<p>Problem https://leetcode.com/problems/swap-salary/description/ Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sql/leetcode-627-swap-salary/">花花酱 LeetCode 627. Swap Salary</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Problem</h1>
<p><a href="https://leetcode.com/problems/swap-salary/description/">https://leetcode.com/problems/swap-salary/description/</a></p>
<div class="question-description">
<div>
<p>Given a table <code>salary</code>, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table.For example:</p>
<pre class="crayon:false">| id | name | sex | salary |
|----|------|-----|--------|
| 1  | A    | m   | 2500   |
| 2  | B    | f   | 1500   |
| 3  | C    | m   | 5500   |
| 4  | D    | f   | 500    |
</pre>
<p>After running your query, the above salary table should have the following rows:</p>
<pre class="crayon:false">| id | name | sex | salary |
|----|------|-----|--------|
| 1  | A    | f   | 2500   |
| 2  | B    | m   | 1500   |
| 3  | C    | f   | 5500   |
| 4  | D    | m   | 500    |
</pre>
</div>
</div>
<h1><strong>Solution: XOR</strong></h1>
<p></p><pre class="crayon-plain-tag"># Author: Huahua
# Running time: 309 ms
update salary set sex = char(ascii('f') ^ ascii('m') ^ ascii(sex))</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sql/leetcode-627-swap-salary/">花花酱 LeetCode 627. Swap Salary</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/sql/leetcode-627-swap-salary/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 596. Classes More Than 5 Students</title>
		<link>https://zxi.mytechroad.com/blog/sql/leetcode-596-classes-more-than-5-students/</link>
					<comments>https://zxi.mytechroad.com/blog/sql/leetcode-596-classes-more-than-5-students/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 03 Apr 2018 05:55:48 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[distinct]]></category>
		<category><![CDATA[having]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2410</guid>

					<description><![CDATA[<p>Problem There is a table courses with columns: student and class Please list out all classes which have more than or equal to 5 students. For example, the table: +---------+------------+&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sql/leetcode-596-classes-more-than-5-students/">花花酱 LeetCode 596. Classes More Than 5 Students</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>There is a table <code>courses</code> with columns: <b>student</b> and <b>class</b></p>
<p>Please list out all classes which have more than or equal to 5 students.</p>
<p>For example, the table:</p>
<pre class="crayon:false">+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
+---------+------------+
</pre>
<p>Should output:</p>
<pre class="crayon:false ">+---------+
| class   |
+---------+
| Math    |
+---------+
</pre>
<p><b>Note:</b><br />
The students should not be counted duplicate in each course.</p>
<h1><strong>Solution</strong></h1>
<p>SQL</p><pre class="crayon-plain-tag"># Author: Huahua
# Running time: 1756 ms
select class from courses group by class having count(distinct student) &gt;= 5</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sql/leetcode-596-classes-more-than-5-students/">花花酱 LeetCode 596. Classes More Than 5 Students</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/sql/leetcode-596-classes-more-than-5-students/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 620. Not Boring Movies</title>
		<link>https://zxi.mytechroad.com/blog/sql/leetcode-620-not-boring-movies/</link>
					<comments>https://zxi.mytechroad.com/blog/sql/leetcode-620-not-boring-movies/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 07 Mar 2018 09:28:07 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2012</guid>

					<description><![CDATA[<p>Problem: https://leetcode.com/problems/not-boring-movies/description/ X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sql/leetcode-620-not-boring-movies/">花花酱 LeetCode 620. Not Boring Movies</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/not-boring-movies/description/">https://leetcode.com/problems/not-boring-movies/description/</a></p>
<p>X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating the movies’ ratings and descriptions.</p>
<p>Please write a SQL query to output movies with an odd numbered ID and a description that is not &#8216;boring&#8217;. Order the result by rating.</p>
<p>For example, table <code>cinema</code>:</p><pre class="crayon-plain-tag">+---------+-----------+--------------+-----------+
|   id    | movie     |  description |  rating   |
+---------+-----------+--------------+-----------+
|   1     | War       |   great 3D   |   8.9     |
|   2     | Science   |   fiction    |   8.5     |
|   3     | irish     |   boring     |   6.2     |
|   4     | Ice song  |   Fantacy    |   8.6     |
|   5     | House card|   Interesting|   9.1     |
+---------+-----------+--------------+-----------+</pre><p>For the example above, the output should be:</p><pre class="crayon-plain-tag">+---------+-----------+--------------+-----------+
|   id    | movie     |  description |  rating   |
+---------+-----------+--------------+-----------+
|   5     | House card|   Interesting|   9.1     |
|   1     | War       |   great 3D   |   8.9     |
+---------+-----------+--------------+-----------+</pre><p>MySQL</p><pre class="crayon-plain-tag"># Author: Huahua
SELECT * FROM cinema 
WHERE id % 2 = 1 
AND description != 'boring' 
ORDER BY rating DESC</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sql/leetcode-620-not-boring-movies/">花花酱 LeetCode 620. Not Boring Movies</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/sql/leetcode-620-not-boring-movies/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
