Basic SQL Concepts - WHERE CLAUSE

 

The WHERE clause lets you add further conditions on your query. We will see the use of WHERE clauses along with other clauses that will help us refine our queries in this section.

 

Commonly asked questions based on concepts in this page:

  1. Give the basic usage of NOT & IN in WHERE clause?
  2. Can we compare null as WHERE id=NULL?
  3. Does range include endpoints when you use BETWEEN and AND in where clause?
  4. What are the wildcards that you can use with LIKE and NOT LIKE? How do you escape if your result can have them?
  5. Give the basic use of HAVING? When is it used? Can we use it along with WHERE?

 

Basic Concepts – WHERE Clause and other clauses

1. The WHERE clause lets you add further conditions on your query.

SELECT * FROM Employees WHERE firstname = ‘Heartin’;

 

2. Use IN clause to specify a set of acceptable values in where clause.

SELECT * FROM mytable WHERE col1 IN(“value1”,”value2”);

 

3. Use AND/OR to combine clauses, and NOT to negate conditions - AND insist all clauses to be true. OR insist any clause to be true.

SELECT * FROM mytable WHERE col1 NOT IN (“value2”,”value3”);

 

4. You can use BETWEEN and AND to filter result. The range include endpoints when you use BETWEEN and AND in where clause.

 

5. The comparison operators that we can use in a WHERE clause are:

·       = (Equality)

·       <> or != (Not equal to)

·       < 

·       <=

·       !< (Not less than)

·       > 

·       >=

·       !>

 

6. SQL don’t do comparisons involving NULL like ‘WHERE id=NULL’. Instead we can use ‘IS NULL’ or ‘IS NOT NULL’.

 

7. We can use LIKE to filter queries based on similarity. The wildcards that you can use with LIKE and NOT LIKE are:

  • ‘-’ (Underscore) -> Stands for any one character
  • ‘%’ -> Stands for any one or more characters.

Below query uses LIKE to find all employees whose first names had 4 characters:

SELECT * FROM Employees WHERE firstname LIKE ‘_ _ _ _’;

To search for terms that include the ‘_’ or ‘%’ characters using LIKE, you can escape those characters. You can put any character of your choice before that and tell SQL that character is an escape character using ESCAPE ‘#’ at the end or use backslash to escape.

SELECT Savings FROM Financials WHERE Savings LIKE ‘10#%’ ESCAPE ‘#’;

SELECT Savings FROM Financials WHERE Savings LIKE ‘10\%’;

The SIMILAR TO clause is similar to LIKE, but we can use regular expressions. Some DBMSs use REGEXP or REGEXP_LIKE instead of SIMILAR TO.

MATCH with WHERE lets you match individual records.

… WHERE (‘heartin’,’jacob’) MATCH (SELECT firstname, lastname FROM Employees);

 

8. The ALL keyword makes SQL checks all results returned from a sub-query. ANY means any result. ‘SOME’ means the same as ‘ANY’.

… WHERE id> ALL(SELECT Supervisor FROM Employees)

 

9. We can filter groups using HAVING clause when using GROUP BY with WHERE. When used together, rule is that the GROUP BY clause must come only after the WHERE clause, but can come before HAVING clause. So HAVING is required with GROUP BY when you want to apply some condition after grouping.

WHERE condition GROUP BY columnName HAVING condition

 

Tags: 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)