{"id":6039,"date":"2020-05-20T11:02:45","date_gmt":"2020-05-20T15:02:45","guid":{"rendered":"http:\/\/springframework.guru\/?p=6039"},"modified":"2020-05-20T11:02:45","modified_gmt":"2020-05-20T15:02:45","slug":"sql-using-the-select-statement","status":"publish","type":"post","link":"https:\/\/springframework.guru\/sql-using-the-select-statement\/","title":{"rendered":"SQL, Using The SELECT Statement"},"content":{"rendered":"<p>In this short tutorial, we&#8217;ll learn the basics of using the <strong>SQL SELECT<\/strong> command. To follow along, you will need to have <strong>MySQL<\/strong> installed on your system, and you will need to install this simple <a href=\"https:\/\/github.com\/springframeworkguru\/test_db\">database<\/a>.<\/p>\n<h2>Select All Columns From a Table<\/h2>\n<p>We use the <strong>&#8220;*&#8221;<\/strong> to select all the columns in a table. <strong>Here is an example<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">SELECT * FROM employees;<\/pre>\n<p>Output:<\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 88px;\">\n<tbody>\n<tr style=\"height: 22px;\">\n<td style=\"width: 16.6667%; height: 22px;\">emp_no<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">birth_date<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">first_name<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">last_name<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">gender<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">hire_date<\/td>\n<\/tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 16.6667%; height: 22px;\">10001<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">1953-09-02<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">Georgi<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">Facello<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">M<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">1986-06-26<\/td>\n<\/tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 16.6667%; height: 22px;\">10002<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">1964-06-02<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">Bezalel<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">Simmel<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">F<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">1985-11-21<\/td>\n<\/tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 16.6667%; height: 22px;\">10003<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">1959-12-03<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">Parto<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">Bamford<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">M<\/td>\n<td style=\"width: 16.6667%; height: 22px;\">1986-08-28<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Select Fewer Columns From a Table<\/h2>\n<p>We can select fewer columns from a table by naming the columns we want to return. <strong>Here is an example that only returns the first and last name from the employees&#8217; table:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">SELECT first_name, last_name FROM employees;<\/pre>\n<p>Output:<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tbody>\n<tr>\n<td style=\"width: 50%;\">first_name<\/td>\n<td style=\"width: 50%;\">last_name<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Georgi<\/td>\n<td style=\"width: 50%;\">Facello<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Bezalel<\/td>\n<td style=\"width: 50%;\">Simmel<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Parto<\/td>\n<td style=\"width: 50%;\">Bamford<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Count Number of Rows in a Table<\/h2>\n<p>To count the number of rows in a table, we use the <strong>Count function:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">SELECT COUNT(*) FROM employees;<\/pre>\n<p>Output:<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tbody>\n<tr>\n<td style=\"width: 100%;\">Count(*)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%;\">300024<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>Select and Where Filter<\/h2>\n<p>We can filter the values that are returned by using the WHERE clause. <strong>Here is an example that only returns employees that are males:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">SELECT * FROM employees\r\nwhere gender = 'M'<\/pre>\n<p>Output:<\/p>\n<table style=\"width: 98.6499%;\">\n<tbody>\n<tr>\n<td style=\"width: 13.1915%;\">emp_no<\/td>\n<td style=\"width: 19.1489%;\">birth_date<\/td>\n<td style=\"width: 17.234%;\">first_name<\/td>\n<td style=\"width: 16.5957%;\">last_name<\/td>\n<td style=\"width: 11.4894%;\">gender<\/td>\n<td style=\"width: 167.79%;\">hire_date<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.1915%;\">10001<\/td>\n<td style=\"width: 19.1489%;\">1953-09-02<\/td>\n<td style=\"width: 17.234%;\">Georgi<\/td>\n<td style=\"width: 16.5957%;\">Facello<\/td>\n<td style=\"width: 11.4894%;\">M<\/td>\n<td style=\"width: 167.79%;\">1986-06-26<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.1915%;\">10003<\/td>\n<td style=\"width: 19.1489%;\">1959-12-03<\/td>\n<td style=\"width: 17.234%;\">Parto<\/td>\n<td style=\"width: 16.5957%;\">Bamford<\/td>\n<td style=\"width: 11.4894%;\">M<\/td>\n<td style=\"width: 167.79%;\">196-08-28<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 13.1915%;\">10004<\/td>\n<td style=\"width: 19.1489%;\">1954-05-01<\/td>\n<td style=\"width: 17.234%;\">Christian<\/td>\n<td style=\"width: 16.5957%;\">Koblick<\/td>\n<td style=\"width: 11.4894%;\">M<\/td>\n<td style=\"width: 167.79%;\">1986-12-01<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>Ordering the Results<\/h2>\n<p>We can order the results base on the column value by using the <strong>ORDER BY <\/strong>statement. <strong>Here is an example that returns employees Order By last names:<\/strong><\/p>\n<p>Output:<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tbody>\n<tr>\n<td style=\"width: 50%;\">first_name<\/td>\n<td style=\"width: 50%;\">last_name<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Basim<\/td>\n<td style=\"width: 50%;\">Aamodt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Peternela<\/td>\n<td style=\"width: 50%;\">Aamodt<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%;\">Gretta<\/td>\n<td style=\"width: 50%;\">Aamodt<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>Conclusion<\/h2>\n<p>In this short tutorial, you learned the basics of using the <strong>SQL SELECT<\/strong> statement. To learn more about SQL, please check out the <a href=\"https:\/\/www.udemy.com\/course\/sql-beginner-to-guru-mysql-edition\/?referralCode=5DDFD6D09E637BB6F4F0\"><strong>MySQL<\/strong><\/a> course.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this short tutorial, we&#8217;ll learn the basics of using the SQL SELECT command. To follow along, you will need to have MySQL installed on your system, and you will need to install this simple database. Select All Columns From a Table We use the &#8220;*&#8221; to select all the columns in a table. Here [&hellip;]<a href=\"https:\/\/springframework.guru\/sql-using-the-select-statement\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":111,"featured_media":4653,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[265],"tags":[132,200],"class_list":["post-6039","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql","tag-mysql","tag-sql"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"jt","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2017\/07\/Banner560x292_08Web.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-1zp","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/6039"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=6039"}],"version-history":[{"count":10,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/6039\/revisions"}],"predecessor-version":[{"id":6106,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/6039\/revisions\/6106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4653"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=6039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=6039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=6039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}