{"id":9907,"date":"2023-03-16T11:45:33","date_gmt":"2023-03-16T16:45:33","guid":{"rendered":"https:\/\/itsoftware.com.co\/content\/?p=9907"},"modified":"2023-03-16T11:46:13","modified_gmt":"2023-03-16T16:46:13","slug":"what-is-database-query","status":"publish","type":"post","link":"https:\/\/itsoftware.com.co\/content\/what-is-database-query\/","title":{"rendered":"What is a database query?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A database query is a standard language that allows you to fetch data from one or more tables, to add, update or delete them, in a fast and powerful way; generally known as SQL language.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/itsoftware.com.co\/content\/wp-content\/uploads\/2015\/06\/sql.png\"><img decoding=\"async\" src=\"https:\/\/itsoftware.com.co\/content\/wp-content\/uploads\/2015\/06\/sql-274x300.png\" alt=\"Consulta o query\" class=\"wp-image-350\"\/><\/a><figcaption class=\"wp-element-caption\">SQL Queries<\/figcaption><\/figure>\n<\/div>\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start with the basic query, which is essentially composed of the following parts:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT \u00a0&lt;fields or columns of a table>\nFROM \u00a0&lt;table or tables>\nWHERE \u00a0&lt;condition to be met by the records>\nORDER BY &lt;fields> ASC\/DESC<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the SELECT reserved word you choose one or more fields or columns that you want to fetch from a table, the symbol * is used to fetch all the columns of a table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the FROM reserved word we put the table or set of tables to which the columns we want to bring in the SELECT belong.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the WHERE clause we indicate the filters, or necessary conditions for the records that we are bringing, for example that they match or not with some necessary comparisons.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And in the ORDER BY reserved word, we put the fields with which we want to sort the results, either in ascending order (ASC) or descending order (DESC).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see this in an example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have the following table of people:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Table name: <strong>t_people<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>ID<\/strong><\/td><td><strong>name<\/strong><\/td><td><strong>lastname<\/strong><\/td><td><strong>person_id<\/strong><\/td><td><strong>age<\/strong><\/td><td><strong>gender<\/strong><\/td><td><strong>incomes<\/strong><\/td><\/tr><tr><td>1<\/td><td>Mario<\/td><td>Solis<\/td><td>4.566.332<\/td><td>15<\/td><td>Male<\/td><td>650.000<\/td><\/tr><tr><td>2<\/td><td>Mireya<\/td><td>Portilla<\/td><td>234.322.323<\/td><td>16<\/td><td>Female<\/td><td>459.000<\/td><\/tr><tr><td>3<\/td><td>Luz<\/td><td>P\u00e9rez<\/td><td>74.654.323<\/td><td>32<\/td><td>Female<\/td><td>2.500.000<\/td><\/tr><tr><td>4<\/td><td>Gonzalo<\/td><td>Navia<\/td><td>8.643.234<\/td><td>23<\/td><td>Male<\/td><td>1.300.000<\/td><\/tr><tr><td>5<\/td><td>Andr\u00e9s<\/td><td>Ram\u00edrez<\/td><td>6.342.345<\/td><td>18<\/td><td>Male<\/td><td>800.000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the following query we are going to get the first and last name of the people with ages over 23 years old, of female gender and in ascending alphabetical order by name:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT name, lastname\nFROM \u00a0t_people\nWHERE age>23 \u00a0 AND\u00a0 gender='Female'\nORDER BY name ASC<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result of this query would be:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>name<\/strong><\/td><td><strong>lastname<\/strong><\/td><td><strong>person_id<\/strong><\/td><td><strong>age<\/strong><\/td><td><strong>gender<\/strong><\/td><td><strong>incomes<\/strong><\/td><\/tr><tr><td>Luz<\/td><td>P\u00e9rez<\/td><td>74.654.323<\/td><td>32<\/td><td>Female<\/td><td>2.500.000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Another example of a query could be to find the total income by gender:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT gender, SUM (incomes) as TOTAL_INCOME\nFROM t_people\nGROUP BY gender<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Whose result would be:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>gender<\/strong><\/td><td><strong>TOTAL_INCOME<\/strong><\/td><\/tr><tr><td>Male<\/td><td>2.750.000<\/td><\/tr><tr><td>Female<\/td><td>2.959.000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In this case the aggregate function SUM is used to perform a sum over a set of records, and the GROUP BY clause to indicate the field by which the sum operation is grouped.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is a simple case of query, later in other articles, we will show more complex and powerful queries for the management and extraction of information from databases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These articles are part of the process of dissemination of knowledge offered by <strong><a href=\"https:\/\/itsoftware.com.co\/content\/data-analytics-company\/\" target=\"_blank\" rel=\"noopener\" title=\"ITSoftware SAS, Data Analytics Company\">ITSoftware SAS<\/a><\/strong>, within its social function of supporting knowledge as part of the development and progress of humanity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A database query is a standard language that allows you to fetch data from one or more tables, to add, update or delete them, in a fast and powerful way;[&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[305,97],"tags":[319,358,360,361,223,182,184,269,359,326],"class_list":["post-9907","post","type-post","status-publish","format-standard","hentry","category-automatizacion","category-bases-de-datos","tag-base-de-datos","tag-consulta-base-de-datos","tag-consultas-sql","tag-diseno-base-de-datos","tag-ms-access","tag-mysql","tag-oracle","tag-postgresql","tag-sql-query","tag-sqlserver"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/itsoftware.com.co\/content\/wp-json\/wp\/v2\/posts\/9907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsoftware.com.co\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsoftware.com.co\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsoftware.com.co\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsoftware.com.co\/content\/wp-json\/wp\/v2\/comments?post=9907"}],"version-history":[{"count":0,"href":"https:\/\/itsoftware.com.co\/content\/wp-json\/wp\/v2\/posts\/9907\/revisions"}],"wp:attachment":[{"href":"https:\/\/itsoftware.com.co\/content\/wp-json\/wp\/v2\/media?parent=9907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsoftware.com.co\/content\/wp-json\/wp\/v2\/categories?post=9907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsoftware.com.co\/content\/wp-json\/wp\/v2\/tags?post=9907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}