{"id":86,"date":"2018-12-05T09:44:15","date_gmt":"2018-12-05T09:44:15","guid":{"rendered":"https:\/\/barkhane.com\/sql\/2018\/12\/05\/sql-expressions\/"},"modified":"2024-03-03T18:05:09","modified_gmt":"2024-03-03T18:05:09","slug":"sql-expressions","status":"publish","type":"post","link":"https:\/\/barkhane.com\/sql\/sql-expressions\/","title":{"rendered":"SQL Expressions"},"content":{"rendered":"<p>SQL expression is a combination of one or more values, operators and SQL functions that results in to a value. These SQL EXPRESSIONs are similar to a formula and they are written in query language. You can also use them to query the database for a specific set of data.<\/p>\n<h3>Syntax<\/h3>\n<p>Consider the basic syntax of the SELECT statement as follows \u2212<\/p>\n<pre class=\"result notranslate\">SELECT column1, column2, columnN \nFROM table_name \nWHERE [CONDITION|EXPRESSION];<\/pre>\n<p>SQL\u00a0expression can be classified into following categories.<\/p>\n<ol>\n<li>Boolean<\/li>\n<li>Numeric<\/li>\n<li>Date<\/li>\n<\/ol>\n<h3><span id=\"sql-boolean-expression\">SQL Boolean Expression<\/span><\/h3>\n<p>SQL Boolean Expression fetches data based on the condition that is mentioned as part of the SQL query. It should fetch just single value when the query is executed. Its syntax is given below.<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">column<\/span> \n<span class=\"hljs-keyword\">FROM<\/span> table_name \n<span class=\"hljs-keyword\">WHERE<\/span> SINGLE_VALUE_MATCHING_EXPRESSION;\n<\/code><\/pre>\n<p>Let us try to understand Boolean expression using a sample table with some data.<\/p>\n<table class=\"tg\">\n<tbody>\n<tr>\n<th class=\"tg-031e\">EMPID<\/th>\n<th class=\"tg-031e\">EMPNAME<\/th>\n<th class=\"tg-031e\">EMPAGE<\/th>\n<th class=\"tg-031e\">EMPSALARY<\/th>\n<\/tr>\n<tr>\n<td class=\"tg-vn4c\">1<\/td>\n<td class=\"tg-vn4c\">A<\/td>\n<td class=\"tg-vn4c\">32<\/td>\n<td class=\"tg-vn4c\">2000<\/td>\n<\/tr>\n<tr>\n<td class=\"tg-031e\">2<\/td>\n<td class=\"tg-031e\">B<\/td>\n<td class=\"tg-031e\">25<\/td>\n<td class=\"tg-031e\">2500<\/td>\n<\/tr>\n<tr>\n<td class=\"tg-vn4c\">3<\/td>\n<td class=\"tg-vn4c\">C<\/td>\n<td class=\"tg-vn4c\">29<\/td>\n<td class=\"tg-vn4c\">3000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We will consider the Employee table mentioned above as an example.<\/p>\n<div>\n<div data-type=\"ad\" data-publisher=\"journaldev.com\" data-format=\"300x250\" data-zone=\"jd_post_mid_300x250\">Example Query:<\/div>\n<\/div>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">SELECT<\/span> EmpName <span class=\"hljs-keyword\">FROM<\/span> Employee <span class=\"hljs-keyword\">WHERE<\/span> EmpId = <span class=\"hljs-number\">1<\/span>;\n<\/code><\/pre>\n<p>The query above will result in single value which is \u201cA\u201d for our query. SQL Boolean expressions should be used with primary keys to make sure you always get only one results. This is required in case of nested\u00a0SQL select\u00a0queries.<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> posts.title, posts.author_id <span class=\"hljs-keyword\">from<\/span> posts, <span class=\"hljs-keyword\">authors<\/span> \n<span class=\"hljs-keyword\">where<\/span> posts.author_id = authors.id <span class=\"hljs-keyword\">and<\/span> \nposts.author_id = (<span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-keyword\">authors<\/span> <span class=\"hljs-keyword\">where<\/span> <span class=\"hljs-keyword\">name<\/span> = <span class=\"hljs-string\">\\'Pankaj\\'<\/span>);\n<\/code><\/pre>\n<p>Above query will return all the posts name and id where author name is Pankaj. Here nested SQL query should always return only one row otherwise you will get error message as\u00a0<code>Subquery returns more than 1 row<\/code>.<\/p>\n<h3><span id=\"sql-numeric-expression\">SQL Numeric Expression<\/span><\/h3>\n<p>SQL Numeric Expression is used for performing mathematical operation in SQL query. Its syntax is as follows:<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">SELECT<\/span> NUMERICAL_EXPRESSION <span class=\"hljs-keyword\">as<\/span> OPERATION_NAME\n<span class=\"hljs-keyword\">FROM<\/span> table_name\n<\/code><\/pre>\n<p>NUMERICAL_EXPRESSION is the mathematical formula for function which will be used in the SQL query.<\/p>\n<p>Let\u2019s try to understand Numeric expression using an example.<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">count<\/span>(*) <span class=\"hljs-keyword\">FROM<\/span> Employee; <span class=\"hljs-comment\">-- 3<\/span>\n<\/code><\/pre>\n<p>The query above will result as 3 because the COUNT(*) function will provide the total count of the rows based on the condition in WHERE clause. For example\u00a0<code>select count(*) from employee where empsalary &gt; 2400;<\/code>\u00a0will return 2.<\/p>\n<p>There are other functions also like sum(), avg(), min(), max() etc. These functions are used for mathematical operations. Let\u2019s see some more example code snippets for SQL numeric expressions.<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">sum<\/span>(EmpSalary) <span class=\"hljs-keyword\">as<\/span> <span class=\"hljs-string\">\\\"Salary\\\"<\/span> <span class=\"hljs-keyword\">FROM<\/span> Employee; <span class=\"hljs-comment\">-- 7500<\/span>\n<span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">min<\/span>(EmpSalary) <span class=\"hljs-keyword\">from<\/span> Employee; <span class=\"hljs-comment\">-- 2000<\/span>\n<span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">max<\/span>(EmpSalary) <span class=\"hljs-keyword\">from<\/span> Employee; <span class=\"hljs-comment\">-- 3000<\/span>\n<span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">sum<\/span>(EmpSalary) <span class=\"hljs-keyword\">from<\/span> Employee; <span class=\"hljs-comment\">-- 7500<\/span>\n<span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">avg<\/span>(EmpSalary) <span class=\"hljs-keyword\">from<\/span> Employee; <span class=\"hljs-comment\">-- 2500.0000<\/span>\n<\/code><\/pre>\n<h3><span id=\"sql-date-expression\">SQL Date Expression<\/span><\/h3>\n<p>SQL Date Expression results in datetime value.<\/p>\n<p>Let\u2019s try to understand Date expression with some sql statements.<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">CURRENT_TIMESTAMP<\/span>; <span class=\"hljs-comment\">-- 2018-01-20 10:32:37<\/span>\n\n<span class=\"hljs-comment\">-- For MYQSL<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">now<\/span>(); <span class=\"hljs-comment\">-- 2018-01-20 10:32:57<\/span>\n\n<span class=\"hljs-comment\">-- For SQL Server<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">GetDate<\/span>();\n\n<span class=\"hljs-comment\">-- For Oracle DB<\/span>\n<span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">sysdate<\/span> <span class=\"hljs-keyword\">from<\/span> Dual; <span class=\"hljs-comment\">-- 20-JAN-18<\/span>\n<\/code><\/pre>\n<p>The above queries will result in current time of the system. Note that every relational database vendor has specific methods to get current date and timestamp values, so there is a slight chance that one function in a database doesn\u2019t work in another database.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL expression is a combination of one or more values, operators and SQL functions that results in to a value. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}