SQL - Syntax pt.1 Flashcards
SQL statements
In SQL * means
All
record
SQL statements
used to select data from a database.
SQL SELECT Statement
SELECT
CustomerName, City FROM Customers;
Syntax:
SELECT
column1, column2, …
FROM table_name;
SQL statements
is used to return only different values.
SQL DISTINCT Clause
SELECT DISTINCT
Country FROM Customers;
Syntax:
SELECT DISTINCT
column1, column2, …
FROM table_name;
SQL statements
used to filter records.
It is used to extract only those records that fulfill a specified condition
SQL WHERE Clause
SELECT * FROM CustomersWHERE
Country=’Mexico’;
Syntax:
SELECT column1, column2, …
FROM table_nameWHERE
condition;
SQL statements
this operator is used when all the conditions are true.
AND operator
SELECT * FROM Customers
WHERE Country = ‘Germany’ AND
Name = ‘Tom’;
Syntax:
SELECT column1, column2, …
FROM table_name
WHERE condition1 AND
condition2 AND
condition3 …;
SQL statements
operator is used if any one of the conditions are true.
OR operator
SELECT *
FROM Customers
WHERE Country = ‘Germany’ OR
Country = ‘Spain’
Syntax:
SELECT column1, column2, …
FROM table_name
WHERE condition1 OR
condition2 OR
condition3 …;
SQL statements
allows you to specify multiple values in a WHERE clause.
SQL IN Clause
SELECT * FROM Customers
WHERE Country IN
(‘Germany’, ‘France’, ‘UK’);
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN
(value1, value2, …);
SQL statements
selects values within a given range
. The values can be numbers, text, or dates.
SQL BETWEEN Clause
SELECT * FROM Products
WHERE Price BETWEEN
10 AND 20;
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN
value1 AND value2;
SQL statements
is used in a WHERE clause to search for a specified pattern
in a column
SQL LIKE Clause
SELECT * FROM Customers
WHERE CustomerName LIKE
‘a%’;
Syntax:
SELECT column1, column2, …
FROM table_name
WHERE columnN LIKE
pattern;
SQL statements
is used to sort the result-set in ascending or descending order.
SQL ORDER BY Clause
SELECT * FROM ProductsORDER BY
Price;
Syntax:
SELECT column1, column2, …
FROM table_nameORDER BY
column1, column2, … ASC|DESC;
SQL statements
thhis statement groups rows that have the same values into summary rows
SQL GROUP BY Clause
SELECT COUNT(CustomerID), Country
FROM CustomersGROUP BY
Country;
Syntax:
SELECT column_name(s)
FROM table_name
WHERE conditionGROUP BY
column_name(s)
ORDER BY column_name(s);
SQL statements
this function returns the number of rows that matches a specified criterion
SQL COUNT Clause
SELECT COUNT
(*)
FROM Products;
Syntax:
SELECT COUNT
(column_name)
FROM table_name
WHERE condition;
SQL statements
this clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
SQL HAVING Clause
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY CountryHAVING
COUNT(CustomerID) > 5;
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)HAVING
condition
ORDER BY column_name(s);
SQL statements
this statement is used to create a new table in a database.
SQL CREATE TABLE Statement
CREATE TABLE
Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Syntax:
CREATE TABLE
table_name (
column1 datatype,
column2 datatype,
column3 datatype,
….
);
SQL statements
this statement is used to drop an existing table in a database.
SQL DROP TABLE Statement
DROP TABLE
Shippers;
Syntax:
DROP TABLE
table_name;