{"id":80,"date":"2017-03-08T05:48:16","date_gmt":"2017-03-08T05:48:16","guid":{"rendered":"http:\/\/abhiandroid.com\/database\/?p=80"},"modified":"2017-03-08T05:48:16","modified_gmt":"2017-03-08T05:48:16","slug":"all-clauses-sqlite","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/database\/all-clauses-sqlite.html","title":{"rendered":"List Of All Clauses In SQLite For Defining Specific Condition"},"content":{"rendered":"<p>Clauses are used to define any specific condition with the commands like select, insert, update, delete, alter.<\/p>\n<hr \/>\n<h4><strong>WHERE CLAUSE IN SQLITE :<\/strong><\/h4>\n<p>Where clause is used to filter out the result, for that define a condition with where clause.If the specified condition is true it return the records. It is used with update, select, alter..etc.<\/p>\n<p><strong><span style=\"color: #008000;\">Syntax<\/span><\/strong><\/p>\n<pre>select column1, column2, columnN \r\nfrom  \r\nwhere &lt;condition&gt;;<\/pre>\n<p><strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><\/p>\n<pre>sqlite&gt; select * from t1;\r\n<strong>Number    Name<\/strong>\r\n ---      ---\r\n 101      Ram\r\n 102      Ram Kumar\r\n 103      Krishan\r\n 104      Ramesh\r\nsqlite&gt; select name from t1 where num=102;\r\n<strong>Name<\/strong>\r\n----\r\nRam Kumar\r\nsqlite&gt;\r\n<\/pre>\n<h4><strong>AND\/ OR Clause<\/strong><\/h4>\n<p>These clause is used to retrieve records or you can say multiple records with the specified condition define which these two clauses.<\/p>\n<p><span style=\"color: #008000;\"><strong>AND :<\/strong><\/span><\/p>\n<p>With &#8216;AND&#8217; clause we can define multiple conditions but it \u00a0turns true if all specified conditions with it stands true. Like we define a [condition1] and [condition2], will be true if both are\u00a0condition1 , \u00a0condition2 are true.<\/p>\n<p><strong><span style=\"color: #008000;\">Syntax<\/span><\/strong><\/p>\n<pre>Where [condition1] AND [condition2] AND [condition3] ... AND [conditionN]\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">Example: \u00a0 EMPLOYEE TABLE<\/span><\/strong><\/p>\n<pre><strong>Eno     Ename   Esalary   Eaddress<\/strong>\r\n---     -----   -------   ------\r\n101     Rajat   50000.0   Karnal\r\n102     Ranjit  56000.0   Karnal\r\n103     Rahul   66000.0   Karnal\r\n104     Ravi    66000.0   Kaithal\r\n105     Ravina  75000.0   Kaithal\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><\/p>\n<pre>sqlite&gt; select Eno from Employee where Eaddress='Karnal';\r\n<strong>Eno<\/strong>\r\n---\r\n101\r\n102\r\n103\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>OR:<\/strong><\/span><\/p>\n<p>With &#8216;OR&#8217; clause we can define multiple conditions but it returns true if any specified conditions with it is true. Like we define a [condition1] and [condition2], will be true if any of the condition either\u00a0condition1 , \u00a0condition2 is true.<\/p>\n<p><strong><span style=\"color: #008000;\">Syntax<\/span><\/strong><\/p>\n<pre>Where [condition1] AND [condition2] OR [condition3] ... OR [conditionN]<\/pre>\n<p><strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><\/p>\n<pre>sqlite&gt; select Eno, Ename from Employee where Eaddress='Karnal' or Esalary=75000;\r\n<strong>Eno     Ename<\/strong>\r\n---     ----- \r\n101     Rajat\r\n102     Ranjit\r\n103     Rahul\r\n105     Ravina\r\n<\/pre>\n<h4><strong>LIKE CLAUSE<\/strong><\/h4>\n<p>Like clause is used to match values using wildcards. The wildcards are the special character which have some meaning for it.<br \/>\n<span style=\"color: #008000;\"><strong>&#8212; Percentage &#8220;%&#8221;<\/strong><\/span> &#8211; This represents one or more characters.<br \/>\n<strong><span style=\"color: #008000;\">&#8212; Underscore &#8220;_&#8221;<\/span><\/strong> &#8211; This represents single character.<\/p>\n<p><strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><br \/>\nIn following example will return records with Ename ending with alphabet &#8216;t&#8217;.<\/p>\n<pre>sqlite&gt; select Esalary from Employee where ename like '%t';\r\n<strong>Esalary<\/strong> \r\n------\r\n50000.0\r\n56000.0\r\n<\/pre>\n<p>In following will return records as starting with &#8216;R&#8217; followed by any letter after that &#8216;v&#8217; than any number of letters.<\/p>\n<pre>sqlite&gt; select Eno ,Ename from Employee where Ename like 'R_v%';\r\n<strong>Eno     Ename<\/strong>\r\n---     ----\r\n104     Ravi\r\n105     Ravina\r\n<\/pre>\n<h4><strong>GLOB CLAUSE<\/strong><\/h4>\n<p>Glob clause is used to match text values against pattern using unix wildcards. Glob clause is case_ sensitive unlike like clause. The wildcards are the special character which have some meaning for it.<br \/>\n<strong><span style=\"color: #008000;\">&#8212; Asterisk &#8220;*&#8221;<\/span><\/strong> &#8211; This represents one or more characters.<br \/>\n<strong><span style=\"color: #008000;\">&#8212; QuestionMark &#8220;?&#8221;<\/span><\/strong> &#8211; This represents single character.<\/p>\n<p><strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><br \/>\nIn following example will return records with Ename ending with alphabet &#8216;t&#8217;.<\/p>\n<pre>sqlite&gt; select Esalary from Employee where ename glob '*t';\r\n<strong>Esalary<\/strong> \r\n------\r\n50000.0\r\n56000.0\r\n<\/pre>\n<p>In following will return records as starting with &#8216;R&#8217; \u00a0followed by any letter after that &#8216;v&#8217; than any number of letters.<\/p>\n<pre>sqlite&gt; select Eno ,Ename from Employee where Ename glob 'R?v*';\r\n<strong>Eno     Ename<\/strong>\r\n---     ----\r\n104     Ravi\r\n105     Ravina\r\n<\/pre>\n<p><strong><span style=\"color: #ff0000;\">Important Note:<\/span> <\/strong>The basic difference between Like and Glob is that Glob is case-sensitive but like operator \u00a0is not further Glob uses unix wildcards.<\/p>\n<h4><strong>LIMIT &amp; OFFSET CLAUSE<\/strong><\/h4>\n<p>In SQLite limit clause basically limits the output, it only display as much rows as defined in it, let&#8217;s see the example.<\/p>\n<p><strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><br \/>\nIn the below example if you see the limit is set to &#8220;3&#8221;, then it will display first 3 records.<\/p>\n<pre>sqlite&gt; select *  from Employee limit 3 ;\r\n<strong>Eno     Ename   Esalary   Eaddress<\/strong>\r\n---     -----   -------   -------\r\n101     Rajat   50000.0   Karnal\r\n102     Ranjit  56000.0   Karnal\r\n103     Rahul   66000.0   Karnal\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>OFFSET CLAUSE<\/strong><\/span><\/p>\n<p>Using Offset with Limit clause will set a offset that after the defined offset the records will display.<\/p>\n<p><strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><br \/>\nIn the below query the limit clause will display 2 records as explained above and the offset will leave defined number of records in it.<\/p>\n<pre>sqlite&gt; select * from employee limit 2 offset 2;\r\n<strong>Eno     Ename   Esalary   Eaddress<\/strong>\r\n---     -----   -------   --------\r\n103     Rahul   66000.0   Karnal\r\n104     Ravi    66000.0   Kaithal\r\n<\/pre>\n<h4><strong>ORDER BY CLAUSE<\/strong><\/h4>\n<p>Order By Clause as the name symbolized it sorts the data is specified order i.e either ascending or descending. By default the order is ascending. You can use multiple columns with order by clause.<\/p>\n<p><strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><br \/>\nIn the below example order by clause is used which is ordering the records by Eaddress.<\/p>\n<pre>sqlite&gt; select Ename, Esalary from employee order by Eaddress ASC;\r\n<strong>Ename   Esalary<\/strong>\r\n-----   -------\r\nRavi    66000.0\r\nRavina  75000.0\r\nRajat   50000.0\r\nRanjit  56000.0\r\nRahul   66000.0\r\n<\/pre>\n<p>In below example we used order by clause which is sorting records in descending order.<\/p>\n<pre>sqlite&gt; select Ename, Esalary from employee order by Eaddress DESC;\r\n<strong>Ename   Esalary<\/strong>\r\n-----   -------\r\nRajat   50000.0\r\nRanjit  56000.0\r\nRahul   66000.0\r\nRavi    66000.0\r\nRavina  75000.0<\/pre>\n<h4><strong>GROUP BY<\/strong><\/h4>\n<p>Group By Clause is used to group put the similar records together. This clause is used with the select query and optionally you can use order by clause with it.<\/p>\n<p><strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><br \/>\nIn the below example we want to know the no of employees from same location. So we used group by with count function, further we add order by clause which is sorting records according to Eno.<\/p>\n<pre>sqlite&gt; select Eaddress, count(Eaddress) as Number_of_Employees from employee group by Eaddress order by Eno;\r\n<strong>Eaddress        Number_of_Employees<\/strong>\r\n-------         -------------------\r\nKarnal                 3\r\nKaithal                2\r\n<\/pre>\n<h4><strong>HAVING CLAUSE<\/strong><\/h4>\n<p>Having clause is used to specify a condition with the group by.<br \/>\n<strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><br \/>\nIn the following example we just count the no of employees from kaithal by grouping them.<\/p>\n<pre>sqlite&gt; select Eaddress, count(Eaddress) as Number_of_Employees from employee group by Eaddress having Eaddress='Kaithal';\r\n<strong>Eaddress        Number_of_Employees<\/strong>\r\n--------        -------------------\r\nKaithal                2\r\n<\/pre>\n<h4><strong>DISTINCT \/ ALL<\/strong><\/h4>\n<p>Distinct as itself explains it display unique data.<br \/>\n<strong><span style=\"color: #008000;\">Usage:<\/span><\/strong><br \/>\nIn below query distinct address records are fetched.<\/p>\n<pre>sqlite&gt; select Distinct Eaddress from employee;\r\n<strong>Eaddress<\/strong>\r\n------\r\nKarnal\r\nKaithal\r\n<\/pre>\n<p>Similarly using <span style=\"color: #008000;\"><strong>ALL<\/strong><\/span>, will display all records:<\/p>\n<pre>sqlite&gt; select All Eaddress from employee;\r\n<strong>Eaddress<\/strong>\r\n--------\r\nKarnal\r\nKarnal\r\nKarnal\r\nKaithal\r\nKaithal<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Clauses are used to define any specific condition with the commands like select, insert, update, delete, alter. WHERE CLAUSE IN SQLITE : Where clause is used to filter out the result, for that define a condition with where clause.If the specified condition is true it return the records. It is used with update, select, alter..etc. &hellip; <a href=\"https:\/\/abhiandroid.com\/database\/all-clauses-sqlite.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">List Of All Clauses In SQLite For Defining Specific Condition<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-archive"],"acf":[],"psp_head":"<title>List Of All Clauses In SQLite For Defining Specific Condition \u2013 Android Database Tutorial In Android Studio: Store Your Data<\/title>\r\n<meta name=\"description\" content=\"Clauses are used to define any specific condition with the commands like select, insert, update, delete, alter.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/database\/all-clauses-sqlite.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}