Basic SQL Statements Flashcards

0
Q

What is SQL?

A

SQL is an ANSI (American National Standard Institute) standard. It is a standard for accessing and manipulating databases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What does SQL stand for?

A

Standard Query Language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What can SQL do?

A

It can:

   - execute queries against a 
     database
   - retrieve data from a database
   - insert records in a database
   - update records in a database
   - delete records from a database
   - create new database
   - create new tables in a database
   - create stored procedures in a 
      database
   - create views in a database
   - set permissions on tables,
     procedures & views
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Database Table?

A

A database most often contains one or more tables. Each table is identified by a name (e.g. “Customer” or “Order”). Tables contain records (rows) with data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the syntax for the SELECT statement?

A

SELECT column_name,column_name
FROM table_name;

Ex.
SELECT * FROM table_name;

•This means to show all records table name•

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the SELECT statement used for?

A

The SELECT statement is used to select data from a database.

The result is stored in a result table, called the result-set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the syntax for the SELECT DISTINCT statement?

A

SELECT DISTINCT column_name,column_name
FROM table_name;

Ex.
SELECT DISTINCT City FROM Customers;

•This means to selects only the distinct values from the “City” columns from the “Customers” table•

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the SELECT DISTINCT statement used for?

A

The DISTINCT keyword is used to return only distinct (different) values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the syntax for the WHERE clause statement?

A

SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;

Ex.
SELECT * FROM Customers
WHERE Country=’Mexico’;

•This means to selects all the customers from the country “Mexico”, in the “Customers” table•

SQL requires single quotes around text values (most database systems will also allow double quotes).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax for the WHERE clause statement for numerical field?

A

SELECT * FROM Customers
WHERE CustomerID=1;

••This means to selects all the customers with 1 for a customer ID in the “Customers” table•

Numeric fields should not be enclosed in quotes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the operators in the WHERE clause?

A

= Equal

<> Not equal. Note: In some
versions of SQL this operator
may be written as !=

>

Greater than

< Less than

> = Greater than or equal

<= Less than or equal

BETWEEN Between an inclusive
range

LIKE Search for a pattern

IN To specify multiple possible.
values for a column

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the the statement for the AND operator?

A

SELECT * FROM Customers
WHERE Country=’Germany’
AND City=’Berlin’;

•This statement selects all customers from the country “Germany” AND the city “Berlin”, in the “Customers” table•

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the the statement for the OR operator?

A

SELECT * FROM Customers
WHERE City=’Berlin’
OR City=’München’;

•This statement selects all customers from the city “Berlin” OR “München”, in the “Customers” table•

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the the statement for the AND & OR operator?

A

SELECT * FROM Customers
WHERE Country=’Germany’
AND (City=’Berlin’ OR City=’München’);

• This statement selects all customers from the country “Germany” AND the city must be equal to “Berlin” OR “München”, in the “Customers” table•

You can also combine AND and OR (use parenthesis to form complex expressions).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the syntax for the ORDER BY keyword statement with the DESC keyword?

A

SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name DESC;

Ex.
SELECT * FROM Customers
ORDER BY Country DESC;

• This statement selects all customers from the “Customers” table, sorted DESCENDING by the “Country” column•

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the syntax for the ORDER BY statement keyword?

A

SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC;

Ex.
SELECT * FROM Customers
ORDER BY Country;

•This statement selects all customers from the “Customers” table, sorted by the “Country” column•

16
Q

What is the ORDER BY keyword statement for combining multiple columns?

A

SELECT * FROM Customers
ORDER BY Country,CustomerName;

•This statement selects all customers from the “Customers” table, sorted by the “Country” and the “CustomerName” column•

17
Q

What is the ORDER BY keyword statement used for?

A

The ORDER BY keyword is used to sort the result-set by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword.

18
Q

What is the WHERE clause used for?

A

The WHERE clause is used to extract only those records that fulfill a specified criterion.

19
Q

What are the AND & OR operators used for?

A

The AND operator displays a record if both the first condition AND the second condition are true.

The OR operator displays a record if either the first condition OR the second condition is true.

20
Q

What is the INSERT INTO statement used for?

A

The INSERT INTO statement is used to insert new records in a table.

21
Q

What is one of the two ways to write the INSERT INTO statement?

A

INSERT INTO table_name
VALUES (value1,value2,value3,…);

The first form does not specify the column names where the data will be inserted, only their values:

Ex.
INSERT INTO Customers (CustomerName, City, Country)
VALUES (‘Cardinal’, ‘Stavanger’, ‘Norway’);

The following SQL statement will insert a new row, but only insert data in the “CustomerName”, “City”, and “Country” columns (and the CustomerID field will of course also be updated automatically):

22
Q

What is the other way to write the INSERT INTO statement?

A

INSERT INTO table_name (column1,column2,column3,…)
VALUES (value1,value2,value3,…);

The second form specifies both the column names and the values to be inserted:

Ex.
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES (‘Cardinal’,’Tom B. Erichsen’,’Skagen 21’,’Stavanger’,’4006’,’Norway’);

This statement inserts a new row in the “Customers” table.

24
Q

What are the SQL statements that QA does not do but should have knowledge of?

A

INSERT INTO, UPDATE & DELETE.

25
Q

What is the syntax for the UPDATE statement?

A

UPDATE table_name
SET column1=value1,column2=value2,…
WHERE some_column=some_value;

Notice the WHERE clause in the SQL UPDATE statement! The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
26
Q

What is the UPDATE statement used for?

A

The UPDATE statement is used to update existing records in a table.

27
Q

What is the syntax for the UPDATE statement keyword?

A

UPDATE table_name
SET column1=value1,column2=value2,…
WHERE some_column=some_value;

Ex.
UPDATE Customers
SET ContactName=’Alfred Schmidt’, City=’Hamburg’
WHERE CustomerName=’Alfreds Futterkiste’;

This statement update the customer “Alfreds Futterkiste” with a new contact person and city.

WARNING: The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

28
Q

What is the DELETE statement used for?

A

The DELETE statement is used to delete records in a table.

29
Q

What is the syntax for the DELETE statement keyword?

A

DELETE FROM table_name
WHERE some_column=some_value;

Ex.
DELETE FROM Customers
WHERE CustomerName=’Alfreds Futterkiste’ AND ContactName=’Maria Anders’;

WARNING: The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

THIS STATEMENT CANNOT BE UNDONE!