SQL - Syntax pt.1 Flashcards
(31 cards)
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;
SQL statements
statement is used to create indexes in tables.
Indexes are used to retrieve data from the database more quickly than otherwise.
SQL CREATE INDEX Statement
CREATE INDEX
idx_lastnameON
Persons (LastName);
Syntax:
CREATE INDEX
index_nameON
table_name (column1, column2, …);
SQL statements
this statement is used to delete an index in a table.
SQL DROP INDEX Statement
Syntax:
ALTER TABLE table_nameDROP INDEX
index_name;
SQL statements
this command deletes the data inside a table, but not the table itself.
SQL TRUNCATE TABLE Statement
TRUNCATE TABLE
Shippers;
Syntax:
TRUNCATE TABLE
table_name;
SQL statements
this statement is used to add, delete, or modify columns in an existing table.
SQL ALTER TABLE Statement
ALTER TABLE
table_nameADD
column_name datatype;
ALTER
TABLE table_nameADD
,DROP
,MODIFY
,RENAME
column_name datatype;
SQL statements
this statement is used to insert new records in a table.
SQL INSERT INTO Statement
INSERT INTO
Customers (CustomerName)
VALUES (‘Tom’);
Syntax:
INSERT INTO
table_name (column1, column2, column3, …)
VALUES (value1, value2, value3, …);
SQL statements
this statement is used to modify the existing records in a table.
SQL UPDATE Statement
UPDATE
Customers
SET ContactName=’Juan’
WHERE Country=’Mexico’;
Syntax:
UPDATE
table_name
SET column1 = value1, column2 = value2, …
WHERE condition;
SQL statements
this statement is used to delete existing records in a table
SQL DELETE Statement
DELETE
FROM Customers WHERE CustomerName=’Alfreds Futterkiste’;
Syntax:
DELETE
FROM table_name WHERE condition;
SQL statements
this statement is used to create a new SQL database.
SQL CREATE DATABASE Statement
CREATE DATABASE
testDB;
Syntax:
CREATE DATABASE
databasename;
SQL statements
this statement is used to drop an existing SQL database.
SQL DROP DATABASE Statement
DROP DATABASE
testDB;
Syntax:
DROP DATABASE
databasename;