SQL Flashcards

1
Q

What is SQL and where is it used?

A

SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the Relational Database Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their standard database language.

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

What are the different dialects of SQL

A

MS SQL Server using T-SQL,
Oracle using PL/SQL,
MS Access version of SQL is called JET SQL (native format) etc

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

What are the functionalities of SQL?

A

Allows users to access data in the relational database management systems.
Allows users to describe the data.
Allows users to define the data in a database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures and views.

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

What commands make up DDL

A

CREATE
ALTER
DROP

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

What commands make up DML?

A

SELECT
INSERT
UPDATE
DELETE

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

What commands make up DCL

A

GRANT

REVOKE

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

Syntax to create a database and then check for it in the list of databases

A

CREATE DATABASE DatabaseName;

SQL> SHOW DATABASES;

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

Syntax to delete database

A

DROP DATABASE DatabaseName;

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

Syntax to choose a certain database before beginning your operations

A

USE DatabaseName;

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

Syntax to create a tabe

A
CREATETABLEPersons (PersonID int,
LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255)
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give a list of the MYSQL data types

A
Varchar
Tinytext
Blob
Int
Tinyint
Bigint
Double
Date
Datetime
Timestamp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Syntax to insert values into the database

A

INSERT INTO TABLE_NAME (column1,column2,column3,…columN)

VALUES (value1,value2,value3,…valueN);

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

Syntax to populate one table using another table

A

INSERT INTO first_table[(column1,column2,…columnN)]
SELECT column1,column2,…,columnN
FROM second_table
[WHERE condition];

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

Syntax for the select statement

A

SELECT column1, column2, columnN FROM table_name;
If you want to fetch all the fields available in the field, then you can use the following syntax.
SELECT * FROM table_name;

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

Syntax of the select ..where statement with both one and many conditions

A

SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000;

*Many conditions
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] AND[condition2]…AND [conditionN];

*Instead of and one may also use OR

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

Syntax of the UPDATE Query

A

UPDATE table_name
SET column1=value1,column2=value2…columnN=valueN
WHERE [condition];

17
Q

Syntax for delete query

A

DELETE FROM CUSTOMERS WHERE ID = 6;

18
Q

How is the like clause used?

A

WHERE Name LIKE ‘a%’

19
Q

What are the different ways of using the wild cards in conjunction with the LIKE operator

(begins with, ends with, in a particular position, in any position, specified number of characters, combination)

A

‘a%’- start with a
‘%a’- end with a
‘%or%’- has the the letters or in any position
r%’- has r in second position
‘a
%_%’ starts with a and has 3 characters in length
‘a%o’- starts with a and ends with o

20
Q

Describe the SQL ORDER BY Clause

A

The SQLORDER BYclause is used to sort the data in ascending or descending order, based on one or more columns. Some databases sort the query results in an ascending order by default.

21
Q

Syntax for the order by clause in both ascending and descending order

A

*ascending
SELECT * FROM CUSTOMERS
ORDER BY NAME, SALARY;

*descending
SELECT * FROM CUSTOMERS
ORDER BY NAME DESC;

22
Q

Describe the SQLGROUP BYclause

A

The SQLGROUP BYclause is used in collaboration with the SELECT statement to arrange identical data into groups. This GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause.

23
Q

Syntax for group by clause

A

SELECT column1, column2
FROM table_name
WHERE [conditions]
GROUP BY column1, column2

24
Q

Describe the SQL DISTINCT keyword and give it’s syntax

A

The SQLDISTINCTkeyword is used in conjunction with the SELECT statement to eliminate all the duplicate records and fetching only unique records.
E.g.
SELECT DISTINCT SALARY FROM CUSTOMERS
ORDER BY SALARY;

25
Q

What functionalities do views give to the user

A
  • Structure data in a way that users or classes of users find natural or intuitive.
  • Restrict access to the data in such a way that a user can see and (sometimes) modify exactly what they need and no more.
  • Summarize data from various tables which can be used to generate reports.
26
Q

What is the syntax for creating a view

A

CREATE VIEW view_name AS
SELECT column 1,column2…
FROM table_name

27
Q

Do exercise in slide 33

A

dfkv

28
Q

Describe SQL null values

A

A field with a NULL value is a field with unknown value
How to Test for NULL Values?
It is not possible to test for NULL values with comparison operators, such as =, .
We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax

29
Q

Give the syntax for the IS NULL and IS NOT NULL statements

A

*is null
SELECT column
FROM table
WHERE column IS NULL

*is not null
SELECT column
FROM table
WHERE column IS NOT NULL

30
Q

Describe the MIN() and MAX() functions and give their syntax

A
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.

MIN() Syntax
SELECT MIN(column_name)
FROM table
WHERE condition;

MAX() Syntax
SELECT MAX(column_name)
FROM table
WHERE condition;

31
Q

Describe the SQL COUNT() function and give its syntax

A
The COUNT() function returns the number of rows that matches a specified criteria.
COUNT() Syntax
SELECTCOUNT(column_name)
FROMtable_name
WHEREcondition;
32
Q

Describe the SQL AVG() and SUM() functions and give their syntax

A
The AVG() function returns the average value of a numeric column.
The SUM() function returns the total sum of a numeric column.
Syntax
SELECTAVG(column_name)
FROMtable_name 
WHEREcondition;
SELECTSUM(column_name)
FROMtable_name
WHEREcondition;
33
Q

Describe the SQL BETWEEN operator and give its syntax

A

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
Example
SELECT*FROMProducts
WHEREPriceBETWEEN10AND20;

34
Q

Do the exercise in https://www.codecademy.com/learn/learn-sql

A

sdn

35
Q

What is the syntax of making primary and foreign keys

A
CREATE TABLE Orders (
    OrderID int NOT NULL,
    OrderNumber int NOT NULL,
    PersonID int,
    PRIMARY KEY (OrderID),
    FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);