SQL5


Select Query

select-statment-1+
Comparison Operators
For numbers (INTDECIMALFLOAT), you could use comparison operators: '=' (equal to), '<>' or '!=' (not equal to), '>' (greater than), '<' (less than), '>=' (greater than or equal to), '<=' (less than or equal to), to compare two numbers. For example, price > 1.0quantity <= 500.Select-statment-2CAUTION: Do not compare FLOATs (real numbers) for equality ('=' or '<>'), as they are not precise. On the other hand, DECIMAL are precise. For strings, you could also use '=''<>''>''<''>=''<=' to compare two strings (e.g., productCode = 'PEC'). The ordering of string depends on the so-called collation chosen. For example,+
Select-statment-3+
String Pattern Matching – LIKE and NOT LIKE
For strings, in addition to full matching using operators like '=' and '<>', we can perform pattern matching using operator LIKE (or NOT LIKE) with wildcard characters. The wildcard '_' matches any single character; '%' matches any number of characters (including zero). For example,+
  • 'abc%' matches strings beginning with 'abc';
  • '%xyz' matches strings ending with 'xyz';
  • '%aaa%' matches strings containing 'aaa';
  • '___' matches strings containing exactly three characters; and
  • 'a_b%' matches strings beginning with 'a', followed by any single character, followed by 'b', followed by zero or more characters.
Select-statment-4+
 +
Note : MySQL also support regular expression matching via the REGEXE operator.+
Logical Operators – ANDORNOTXOR
You can combine multiple conditions with boolean operators ANDORXOR. You can also invert a condition using operator NOT. For example,+
select-using-AND+
INNOT IN
BETWEENNOT BETWEEN
IS NULLIS NOT NULL
select-IN-Between-Null+
ORDER BY Clause & LIMIT Clause
Order-by-Limit-clause+
 +
AS – Alias
You could use the keyword AS to define an alias for an identifier (such as column name, table name). The alias will be used in displaying the name. It can also be used as reference. For example,+
as-alias+
 +
Function CONCAT()
concat-fucntion-in-sql+
 +

Producing Summary Reports

To produce a summary report, we often need to aggregate related rows.+
DISTINCT
A column may have duplicate values, we could use keyword DISTINCT to select only distinct values. We can also apply DISTINCT to several columns to select distinct combinations of these columns. For example,+
distinct-in-sql+
 +
GROUP BY Clause
The GROUP BY clause allows you to collapse multiple records with a common value into groups. For example,+
group-by-clause+
 +
GROUP BY by itself is not meaningful. It is used together with GROUP BY aggregate functions (such as COUNT()AVG()SUM()) to produce group summary.+
GROUP BY Aggregate Functions: COUNTMAXMINAVGSUMSTDGROUP_CONCAT
count+
 +
Besides COUNT(), there are many other GROUP BY aggregate functions such as AVG()MAX()MIN() and SUM(). For example,+
max-min-avg-sum-in-sql+
HAVING clause  &  WITH ROLLUP
HAVING is similar to WHERE, but it can operate on the GROUP BY aggregate functions; whereas WHERE operates only on columns.+
having-and-roll-up-in-SQL+

Comments

Popular posts from this blog

Java Beginners Tutotrial