simple queries Flashcards

1
Q

select in descending order

A

SELECT Name
FROM Student
WHERE course IN [‘C1’, ‘C2’,C4]
ORDER BY Name DESC, course ASC

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

group with count example

A

SELECT Speciality, Count ()
FROM Student
GROUP BY Speciality
HAVING COUNT (
) > 2

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

Update example

A

UPDATE myDB.students
SET fname = ‘Captain’, lname = ‘America’
WHERE student_id =

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

UNION

A

SELECT name FROM Students
UNION
SELECT name FROM Contacts;

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

UNION with duplicates

A

SELECT name FROM Students
UNION ALL
SELECT name FROM Contacts;

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

MINUS

A

SELECT name FROM Students
MINUS
SELECT name FROM Contacts;

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

INTERSECT

A

SELECT name FROM Students
INTERSECT
SELECT name FROM Contacts;

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

cursor

A

DECLARE @name VARCHAR(50) /* Declare All Required Variables */

DECLARE db_cursor CURSOR FOR /* Declare Cursor Name*/
SELECT name
FROM myDB.students
WHERE parent_name IN (‘Sara’, ‘Ansh’)

OPEN db_cursor /* Open cursor and Fetch data into @name */
FETCH next
FROM db_cursor
INTO @name

CLOSE db_cursor /* Close the cursor and deallocate the resources */
DEALLOCATE db_cursor

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

alias example

A

SELECT A.emp_name AS “Employee” /* Alias using AS keyword /
B.emp_name AS “Supervisor”
FROM employee A, employee B /
Alias without AS keyword */
WHERE A.emp_sup = B.emp_id;

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

Truncate

A

TRUNCATE command is used to delete all the rows from the table and free the space containing the table.
TRUNCATE TABLE Candidates;

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

Aggregate functions

A

AVG() - Calculates the mean of a collection of values.
COUNT() - Counts the total number of records in a specific table or view.
MIN() - Calculates the minimum of a collection of values.
MAX() - Calculates the maximum of a collection of values.
SUM() - Calculates the sum of a collection of values.
FIRST() - Fetches the first element in a collection of values.
LAST() - Fetches the last element in a collection of values.

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

Scalar functions

A

LEN() - Calculates the total length of the given field (column).
UCASE() - Converts a collection of string values to uppercase characters.
LCASE() - Converts a collection of string values to lowercase characters.
MID() - Extracts substrings from a collection of string values in a table.
CONCAT() - Concatenates two or more strings.
RAND() - Generates a random collection of numbers of given length.
ROUND() - Calculates the round off integer value for a numeric field (or decimal point values).
NOW() - Returns the current data & time.
FORMAT() - Sets the format to display a collection of values

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

Using the % wildcard to perform a simple search

A

SELECT *
FROM students
WHERE first_name LIKE ‘K%’

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

Omitting the patterns using the NOT keyword

A

SELECT *
FROM students
WHERE first_name NOT LIKE ‘K%’

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

Using the _ wildcard to match pattern at a specific position

A

SELECT *
FROM students
WHERE first_name LIKE ‘__K%’

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

Matching patterns for specific length

A

SELECT * /* Matches first names with exactly four characters */
FROM students
WHERE first_name LIKE ‘____’