-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
212 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Learn SQL by example on SQL by example</title><link>https://peyman.blog/sql/</link><description>Recent content in Learn SQL by example on SQL by example</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><managingEditor>[email protected] (Peyman Salehi)</managingEditor><webMaster>[email protected] (Peyman Salehi)</webMaster><lastBuildDate>Sat, 03 Feb 2024 15:49:57 +0330</lastBuildDate><atom:link href="https://peyman.blog/sql/index.xml" rel="self" type="application/rss+xml"/><item><title>MIN, MAX</title><link>https://peyman.blog/sql/posts/min-max/</link><pubDate>Sat, 03 Feb 2024 15:49:57 +0330</pubDate><author>[email protected] (Peyman Salehi)</author><guid>https://peyman.blog/sql/posts/min-max/</guid><description>MIN returns the smallest value of a column. MAX returns the biggest value of a column. | ||
<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Learn SQL by example on SQL by example</title><link>https://peyman.blog/sql/</link><description>Recent content in Learn SQL by example on SQL by example</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><managingEditor>[email protected] (Peyman Salehi)</managingEditor><webMaster>[email protected] (Peyman Salehi)</webMaster><lastBuildDate>Mon, 05 Feb 2024 11:17:10 +0330</lastBuildDate><atom:link href="https://peyman.blog/sql/index.xml" rel="self" type="application/rss+xml"/><item><title>LIKE</title><link>https://peyman.blog/sql/posts/like/</link><pubDate>Mon, 05 Feb 2024 11:17:10 +0330</pubDate><author>[email protected] (Peyman Salehi)</author><guid>https://peyman.blog/sql/posts/like/</guid><description>LIKE is use in WHERE condition to find data based on a pattern. | ||
There are two wildcards that we can use with LIKE: | ||
%: represents zero, one, or multiple characters _: represents one, single character. -- find all users with first_name of John SELECT id, first_name, last_name FROM users WHERE first_name LIKE &#39;John&#39;; -- +-----+------------+-----------+ -- | id | first_name | last_name | -- |-----+------------+-----------| -- | 14 | John | Combs | -- | 51 | John | Fritz | -- | 285 | John | Sanders | -- | 445 | John | Wise | -- | 485 | John | Peterson | -- | 501 | John | Moreno | -- | 502 | John | Moreno | -- +-----+------------+-----------+ -- find all users that their first_name starts with A SELECT id, first_name, last_name FROM users WHERE first_name LIKE &#39;A%&#39;; -- +-----+------------+-----------+ -- | id | first_name | last_name | -- |-----+------------+-----------| -- | 13 | Andrew | Mercado | -- | 42 | Adrian | Ewing | -- | 74 | Audrey | Edwards | -- find all users that their first_name ends with n SELECT id, first_name, last_name FROM users WHERE first_name LIKE &#39;%n&#39;; -- +-----+------------+-----------+ -- | id | first_name | last_name | -- |-----+------------+-----------| -- | 1 | Carmen | Malone | -- | 3 | Brandon | Jenkins | -- | 7 | Jocelyn | Williams | -- | 10 | Steven | Perez | -- .</description></item><item><title>AS</title><link>https://peyman.blog/sql/posts/as/</link><pubDate>Mon, 05 Feb 2024 11:01:38 +0330</pubDate><author>[email protected] (Peyman Salehi)</author><guid>https://peyman.blog/sql/posts/as/</guid><description>AS keyword is use to make an alias for a column name. | ||
SELECT COUNT(id), AVG(total_price), user_id FROM orders GROUP BY user_id; -- V V -- +-------+-----------------------+---------+ -- | count | avg | user_id | -- |-------+-----------------------+---------| -- | 1 | 2220.0000000000000000 | 384 | -- | 3 | 1319.3333333333333333 | 351 | -- Use alias for count and avg columns SELECT COUNT(id) as number_of_orders, AVG(total_price) avg_total_price, user_id FROM orders GROUP BY user_id; -- V V -- +------------------+-----------------------+---------+ -- | number_of_orders | avg_total_price | user_id | -- |------------------+-----------------------+---------| -- | 1 | 2220.</description></item><item><title>COUNT, SUM, AVG</title><link>https://peyman.blog/sql/posts/count-sum-avg/</link><pubDate>Sun, 04 Feb 2024 13:45:07 +0330</pubDate><author>[email protected] (Peyman Salehi)</author><guid>https://peyman.blog/sql/posts/count-sum-avg/</guid><description>COUNT returns the number of rows for a column. | ||
-- return the number of all users SELECT COUNT(*) FROM users; -- +-------+ -- | count | -- |-------| -- | 502 | -- +-------+ -- find the number of orders by each user SELECT COUNT(*), user_id FROM orders GROUP BY user_id ORDER BY user_id; -- +-------+---------+ -- | count | user_id | -- |-------+---------| -- | 2 | 1 | -- | 1 | 2 | -- | 3 | 3 | -- | 4 | 4 | -- .</description></item><item><title>MIN, MAX</title><link>https://peyman.blog/sql/posts/min-max/</link><pubDate>Sat, 03 Feb 2024 15:49:57 +0330</pubDate><author>[email protected] (Peyman Salehi)</author><guid>https://peyman.blog/sql/posts/min-max/</guid><description>MIN returns the smallest value of a column. MAX returns the biggest value of a column. | ||
-- return the lowest total_price from orders of user with id of 10 SELECT MIN(total_price) FROM orders WHERE user_id = 10; -- +------+ -- | min | -- |------| -- | 1132 | -- +------+ -- return the highest total_price from the orders of user with id of 10 SELECT MAX(total_price) FROM orders WHERE user_id = 10; -- +------+ -- | max | -- |------| -- | 1998 | -- +------+ PostgreSQL docs</description></item><item><title>Comment</title><link>https://peyman.blog/sql/posts/comment/</link><pubDate>Sat, 03 Feb 2024 15:11:15 +0330</pubDate><author>[email protected] (Peyman Salehi)</author><guid>https://peyman.blog/sql/posts/comment/</guid><description>When you writing multiline SQL queries you can use -- to comment a single line and use /* */ to comment multiline texts. | ||
-- Single line comment /* Multi line comment */</description></item><item><title>LIMIT</title><link>https://peyman.blog/sql/posts/limit/</link><pubDate>Fri, 02 Feb 2024 15:09:44 +0330</pubDate><author>[email protected] (Peyman Salehi)</author><guid>https://peyman.blog/sql/posts/limit/</guid><description>When the number of rows in a select query are a lot, we can limit them by the LIMIT statement. | ||
-- only return 3 item SELECT * FROM users LIMIT 3; -- +----+---------------------+------------+-----------+--------------------------------+----------------+--------+ -- | id | datetime_joined | first_name | last_name | email | city | active | -- |----+---------------------+------------+-----------+--------------------------------+----------------+--------| -- | 1 | 2023-08-18 21:44:13 | Carmen | Malone | [email protected] | East Jeanmouth | True | -- | 2 | 2023-07-27 04:31:11 | Stephanie | Wallace | hjennings@curry.</description></item><item><title>AND, OR, NOT</title><link>https://peyman.blog/sql/posts/and-or-not/</link><pubDate>Fri, 02 Feb 2024 14:18:01 +0330</pubDate><author>[email protected] (Peyman Salehi)</author><guid>https://peyman.blog/sql/posts/and-or-not/</guid><description>With the help of AND, OR, and NOT we can add more power to our query conditions. | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!doctype html><html lang=en><head><meta charset=utf-8><meta http-equiv=x-ua-compatible content="ie=edge"><title>AS - SQL by example</title><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=https://peyman.blog/sql/favicon.png><link rel=alternate type=application/rss+xml href=https://peyman.blog/sql/index.xml title="SQL by example"><link rel=stylesheet href=../../css/style.min.b7a8f934af918fc60a41bae619210b3560306dbdbba86726567d6993bc1bc64c.css><meta property="og:title" content="AS"><meta property="og:type" content="website"><meta property="og:url" content="https://peyman.blog/sql/posts/as/"><meta name=twitter:card content="summary"><meta name=twitter:site content="@_peymanslh"><meta name=twitter:creator content="@_peymanslh"><link rel=preconnect href=https://fonts.googleapis.com><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,500;0,700;1,500&family=Libre+Caslon+Text:wght@700&family=Pridi:wght@300;400;600;700&display=swap" rel=stylesheet></head><body class="page page-blog-single"><div id=wrapper class=wrapper><div class=header><a class=header-logo href=https://peyman.blog/sql/>SQL by example</a><div class=menu-main><ul><li class=menu-item-about><a href=../../pages/about/>About</a></li><li class=menu-item-posts><a href=../../posts/>Posts</a></li></ul></div></div><div class=blog><div class=intro><h1>AS<span class=dot></span></h1><ul class=post-tags><li><a href=https://peyman.blog/sql/tags/query/>query</a></li><li><a href=https://peyman.blog/sql/tags/postgresql/>postgresql</a></li></ul></div><div class=content><p><code>AS</code> keyword is use to make an alias for a column name.</p><div class=highlight><pre style=color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-sql data-lang=sql><span style=color:#81a1c1;font-weight:700>SELECT</span> | ||
<span style=color:#81a1c1;font-weight:700>COUNT</span><span style=color:#eceff4>(</span>id<span style=color:#eceff4>),</span> | ||
<span style=color:#81a1c1;font-weight:700>AVG</span><span style=color:#eceff4>(</span>total_price<span style=color:#eceff4>),</span> | ||
user_id | ||
<span style=color:#81a1c1;font-weight:700>FROM</span> orders | ||
<span style=color:#81a1c1;font-weight:700>GROUP</span> <span style=color:#81a1c1;font-weight:700>BY</span> user_id<span style=color:#eceff4>;</span> | ||
<span style=color:#616e87;font-style:italic>-- V V | ||
</span><span style=color:#616e87;font-style:italic>-- +-------+-----------------------+---------+ | ||
</span><span style=color:#616e87;font-style:italic>-- | count | avg | user_id | | ||
</span><span style=color:#616e87;font-style:italic>-- |-------+-----------------------+---------| | ||
</span><span style=color:#616e87;font-style:italic>-- | 1 | 2220.0000000000000000 | 384 | | ||
</span><span style=color:#616e87;font-style:italic>-- | 3 | 1319.3333333333333333 | 351 | | ||
</span><span style=color:#616e87;font-style:italic></span> | ||
<span style=color:#616e87;font-style:italic>-- Use alias for count and avg columns | ||
</span><span style=color:#616e87;font-style:italic></span><span style=color:#81a1c1;font-weight:700>SELECT</span> | ||
<span style=color:#81a1c1;font-weight:700>COUNT</span><span style=color:#eceff4>(</span>id<span style=color:#eceff4>)</span> <span style=color:#81a1c1;font-weight:700>as</span> number_of_orders<span style=color:#eceff4>,</span> | ||
<span style=color:#81a1c1;font-weight:700>AVG</span><span style=color:#eceff4>(</span>total_price<span style=color:#eceff4>)</span> avg_total_price<span style=color:#eceff4>,</span> | ||
user_id | ||
<span style=color:#81a1c1;font-weight:700>FROM</span> orders | ||
<span style=color:#81a1c1;font-weight:700>GROUP</span> <span style=color:#81a1c1;font-weight:700>BY</span> user_id<span style=color:#eceff4>;</span> | ||
<span style=color:#616e87;font-style:italic>-- V V | ||
</span><span style=color:#616e87;font-style:italic>-- +------------------+-----------------------+---------+ | ||
</span><span style=color:#616e87;font-style:italic>-- | number_of_orders | avg_total_price | user_id | | ||
</span><span style=color:#616e87;font-style:italic>-- |------------------+-----------------------+---------| | ||
</span><span style=color:#616e87;font-style:italic>-- | 1 | 2220.0000000000000000 | 384 | | ||
</span><span style=color:#616e87;font-style:italic>-- | 3 | 1319.3333333333333333 | 351 | | ||
</span></code></pre></div></div></div><div class=footer><div class=footer-copy>© 2024 SQL by example.</div><div class=footer-social><span class="social-icon social-icon-github"><a href=https://github.com/peymanslh/sql title=github target=_blank rel=noopener><img src=../../images/social/github.svg width=24 height=24 alt=github></a></span></div></div></div><script type=text/javascript src=../../js/bundle.min.1ff4c3efa6718e4b48bf69ba81bb9b232f6f3e1407b7ea6ce8e5a6fdaed5af56.js></script></body></html> |
Oops, something went wrong.