3-MySQL Flashcards

1-Intro to MySQL 2-SQL Statements 3-Query Results 4-SQL Operators

1
Q

What is the purpose of the CREATE DATABASE SQL command?
A) To delete an existing database
B) To update data in the database
C) To create a new database
D) To retrieve data from the database

A

C) To create a new database

Explanation: The CREATE DATABASE command in SQL is used to create a new database within a MySQL/MariaDB system.

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

How can you view the list of databases in MySQL?
A) LIST DATABASES
B) SHOW DATABASES
C) DISPLAY DATABASES
D) FIND DATABASES

A

B) SHOW DATABASES

Explanation: The SHOW DATABASES command is used in MySQL to display all the databases managed by the database server.

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

Which data type would you use to store a username in MySQL?
A) INT
B) DATE
C) VARCHAR
D) FLOAT

A

Which data type would you use to store a username in MySQL?
A) INT
B) DATE
C) VARCHAR
D) FLOAT

Explanation: The VARCHAR data type is appropriate for storing strings such as usernames, allowing for variable-length entries.

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

What is the standard MySQL/MariaDB port number?
A) 1521
B) 3306
C) 1433
D) 5432

A

B) 3306

Explanation: The default port for MySQL/MariaDB is 3306, although it can be configured to operate on a different port.

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

What keyword is used to automatically increment a column’s value in MySQL?
A) INCREMENT ALWAYS
B) AUTO_INCREMENT
C) INCREASE
D) AUTO_ADD

A

B) AUTO_INCREMENT

Explanation: The AUTO_INCREMENT keyword in MySQL is used to have a column automatically increase its value with each new record inserted.

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

Which command is used to view the structure of a table in MySQL?
A) VIEW TABLE
B) SHOW TABLE
C) DESCRIBE
D) EXPLAIN TABLE

A

C) DESCRIBE

Explanation: The DESCRIBE command is used to display the structure of a table, including its fields and their data types.

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

Quiz 7: What does SQL stand for?
A) Structured Query Language
B) Simple Query Language
C) Standard Query Language
D) Secure Query Language

A

A) Structured Query Language

Explanation: SQL stands for Structured Query Language, a standardized programming language used for managing relational databases.

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

How do you log in to a MySQL database from the command line?
A) mysql -u [username] -p[password]
B) mysql -user [username] -password [password]
C) connect mysql [username] [password]
D) login -u [username] -p [password]

A

A) mysql -u [username] -p[password]

Explanation: To log in to MySQL from the command line, you use the mysql -u [username] -p command, and then you are prompted to enter the password.

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

Which SQL command changes the active database?
A) CHANGE DATABASE
B) SWITCH DATABASE
C) SELECT DATABASE
D) USE

A

D) USE

Explanation: The USE command in SQL is used to switch the active database to the one specified in the command.

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

What does the NOT NULL constraint ensure in an SQL table?
A) The column must always have a numeric value
B) The column can never have a duplicate value
C) The column must always have some value and cannot be left blank
D) The column automatically increments its value

A

C) The column must always have some value and cannot be left blank

Explanation: The NOT NULL constraint in SQL ensures that a column cannot be left empty and must always have a value.

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

What does the INSERT statement do in SQL?
A) Deletes records from a table
B) Adds new records to a table
C) Modifies existing records in a table
D) Lists all records in a table

A

B) Adds new records to a table

Explanation: The INSERT statement is used to add new records to an existing table in a database.

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

What is the function of the SELECT statement in SQL?
A) To modify data in a table
B) To delete data from a table
C) To retrieve data from a table
D) To create a new table

A

C) To retrieve data from a table

Explanation: The SELECT statement is used to query and retrieve data from a database, allowing you to select one or more rows from one or more tables.

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

How do you insert multiple records into a table in a single query?
A) Using multiple INSERT statements
B) Using a JOIN clause
C) By separating values with a comma in the INSERT statement
D) By using the UPDATE statement

A

C) By separating values with a comma in the INSERT statement

Explanation: Multiple records can be inserted in one go by separating each record’s values with a comma within the INSERT statement.

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

What SQL statement would you use to remove a table from a database?
A) REMOVE TABLE
B) DELETE TABLE
C) DROP TABLE
D) ERASE TABLE

A

C) DROP TABLE

Explanation: The DROP TABLE statement is used to delete a table and all of its data from the database.

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

Which SQL command is used to modify the structure of a table?
A) CHANGE TABLE
B) MODIFY TABLE
C) ALTER TABLE
D) UPDATE TABLE

A

C) ALTER TABLE

Explanation: The ALTER TABLE command is used to change the structure of an existing table, such as adding, deleting, or modifying columns.

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

What clause is necessary in an UPDATE statement to specify which records should be modified?
A) SELECT
B) WHERE
C) WHEN
D) HAVING

A

B) WHERE

Explanation: The WHERE clause in an UPDATE statement specifies the conditions that must be met for the records to be updated.

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

How would you view all the columns for all records in a table named employees?
A) SELECT * FROM employees
B) SHOW * FROM employees
C) LIST * FROM employees
D) DISPLAY * FROM employees

A

A) SELECT * FROM employees

Explanation: The SELECT * FROM table_name syntax is used to select all columns from all records in the specified table.

18
Q

What is the default behavior when inserting passwords directly into a database table without hashing?
A) The passwords are automatically encrypted by SQL.
B) It’s considered a best practice for security.
C) The passwords are stored in cleartext, which is a bad practice.
D) The passwords are compressed for storage efficiency.

A

C) The passwords are stored in cleartext, which is a bad practice.

Explanation: Storing passwords in cleartext (unencrypted) is considered a bad practice because it exposes sensitive information to potential security risks.

19
Q

What SQL command is used to add a new column to an existing table?
A) ADD COLUMN in CREATE TABLE
B) INSERT COLUMN
C) ALTER TABLE ADD
D) UPDATE TABLE ADD COLUMN

A

C) ALTER TABLE ADD

Explanation: To add a new column to an existing table, you would use the ALTER TABLE command followed by ADD.

20
Q

If you want to change the data type of a column in a table, which SQL command would you use?
A) CHANGE COLUMN
B) ALTER TABLE MODIFY
C) UPDATE TABLE
D) MODIFY DATA TYPE

A

B) ALTER TABLE MODIFY

Explanation: The ALTER TABLE MODIFY command is used to change the data type of a column in an existing table.

21
Q

What is the purpose of the ORDER BY clause in SQL?
A) To delete records in a specified order
B) To update records in a specified order
C) To select records in a specified order
D) To limit the number of records returned

A

C) To select records in a specified order

Explanation: The ORDER BY clause in SQL is used to sort the results of a query in either ascending or descending order based on one or more columns.

22
Q

How do you sort query results in descending order by a column named age?
A) SELECT * FROM people ORDER BY age ASC
B) SELECT * FROM people ORDER BY age DESC
C) SELECT * FROM people SORT BY age
D) SELECT * FROM people LIMIT age DESC

A

B) SELECT * FROM people ORDER BY age DESC

Explanation: To sort the results in descending order by the age column, you would use the ORDER BY age DESC clause in your SELECT statement.

23
Q

What SQL clause is used to limit the number of records returned by a query?
A) LIMIT
B) WHERE
C) RESTRICT
D) TOP

A

A) LIMIT

Explanation: The LIMIT clause is used in SQL to specify the maximum number of records the query should return.

24
Q

How can you retrieve only the first 5 records from a table called employees?
A) SELECT * FROM employees LIMIT 5
B) SELECT * FROM employees WHERE count=5
C) SELECT TOP 5 * FROM employees
D) SELECT * FROM employees FETCH FIRST 5 ROWS ONLY

A

A) SELECT * FROM employees LIMIT 5

Explanation: To retrieve only the first 5 records from the employees table, use SELECT * FROM employees LIMIT 5.

25
Q

What is the purpose of the WHERE clause in SQL?
A) To specify the tables to select from
B) To define the columns to select
C) To conditionally select data based on specific criteria
D) To sort the output of a query

A

C) To conditionally select data based on specific criteria

Explanation: The WHERE clause is used in SQL to filter records and retrieve only those that meet a specified condition.

26
Q

How do you select rows where the username is exactly ‘admin’?
A) SELECT * FROM users WHERE username = ‘admin’
B) SELECT * FROM users WHERE username == ‘admin’
C) SELECT * FROM users LIMIT username = ‘admin’
D) SELECT * FROM users ORDER BY username = ‘admin’

A

A) SELECT * FROM users WHERE username = ‘admin’

Explanation: To select rows where the username is ‘admin’, the correct SQL syntax is SELECT * FROM users WHERE username = ‘admin’.

27
Q

What does the LIKE clause in SQL do?
A) Deletes records that match a pattern
B) Updates records to match a pattern
C) Selects records that match a pattern
D) Limits the number of records to those that match a pattern

A

C) Selects records that match a pattern

Explanation: The LIKE clause is used in SQL to select records that match a specific pattern, often used with wildcard characters like % or _.

28
Q

Which wildcard character in SQL matches a single character?
A) %
B) #
C) _
D) *

A

C) _

Explanation: In SQL, the _ wildcard character is used to match any single character in a LIKE clause.

29
Q

How can you use ORDER BY to sort by multiple columns?
A) SELECT * FROM employees ORDER BY department, salary DESC
B) SELECT * FROM employees ORDER BY department AND salary DESC
C) SELECT * FROM employees ORDER BY department THEN salary DESC
D) SELECT * FROM employees MULTIORDER BY department, salary DESC

A

A) SELECT * FROM employees ORDER BY department, salary DESC

Explanation: To sort by multiple columns, list the columns separated by commas in the ORDER BY clause, specifying ASC or DESC for each column as needed.

30
Q

How do you use an offset with the LIMIT clause to skip the first record and take the next two?
A) SELECT * FROM employees LIMIT 1 OFFSET 2
B) SELECT * FROM employees LIMIT 2 OFFSET 1
C) SELECT * FROM employees LIMIT 2, 1
D) SELECT * FROM employees OFFSET 1 LIMIT 2

A

B) SELECT * FROM employees LIMIT 2 OFFSET 1

Explanation: To skip the first record and fetch the next two, use LIMIT 2 OFFSET 1, where 2 is the number of records to return and 1 is the number of records to skip.

31
Q

What result is returned by the SQL expression 1 = 1 AND ‘test’ = ‘abc’?
A) 1
B) 0
C) NULL
D) Error

A

B) 0

Explanation: The AND operator returns true only if both conditions are true. Since ‘test’ = ‘abc’ is false, the overall expression evaluates to false, which is represented as 0 in SQL.

32
Q

Which SQL operator is used to test for more than one condition where both conditions must be true for the row to be included in the results?
A) OR
B) AND
C) NOT
D) NOR

A

B) AND

Explanation: The AND operator is used in SQL to combine multiple conditions, and all conditions must be true for the row to be included in the results.

33
Q

What does the NOT operator do in an SQL query?
A) It ensures that both conditions are true.
B) It negates a boolean value, turning true to false and vice versa.
C) It combines two conditions and returns true if either is true.
D) It subtracts one condition from another.

A

B) It negates a boolean value, turning true to false and vice versa.

Explanation: The NOT operator in SQL is used to negate a boolean condition, turning true into false and false into true.

34
Q

How would you write an SQL query to find rows where the column ‘age’ is either greater than 30 or less than 20?
A) SELECT * FROM people WHERE age > 30 AND age < 20;
B) SELECT * FROM people WHERE age > 30 OR age < 20;
C) SELECT * FROM people WHERE NOT age BETWEEN 20 AND 30;
D) Both B and C are correct.

A

D) Both B and C are correct.

Explanation: Both options B and C correctly use SQL logical operators to find rows where ‘age’ is either greater than 30 or less than 20. B uses the OR operator directly, and C uses NOT with BETWEEN to achieve the same effect.

35
Q

Which of the following is the correct way to use the SQL OR operator?
A) SELECT * FROM users WHERE id = 10 OR id = 20;
B) SELECT * FROM users WHERE id = 10 AND id = 20;
C) SELECT * FROM users WHERE id = 10 NOR id = 20;
D) SELECT * FROM users WHERE id = 10 XOR id = 20;

A

A) SELECT * FROM users WHERE id = 10 OR id = 20;

Explanation: The OR operator is used to specify that any of the conditions separated by OR should be true for the rows to be included in the result set.

36
Q

What is the result of SELECT NOT 1 = 1;?
A) 1
B) 0
C) NULL
D) Error

A

B) 0

Explanation: The NOT operator inverses the result of the comparison. Since 1 = 1 is true, NOT 1 = 1 results in false, which is represented as 0 in SQL.

37
Q

In SQL, how is the NOT operator symbolically represented?
A) !
B) -
C) ^
D) %

A

A) !

Explanation: The NOT operator can be symbolically represented as ! in SQL, especially in systems that support symbol-based logical operators.

38
Q

Which SQL statement uses symbol operators correctly?
A) SELECT * FROM products WHERE price > 100 && quantity < 10;
B) SELECT * FROM products WHERE price > 100 || quantity < 10;
C) Both A and B
D) Neither A nor B

A

C) Both A and B

Explanation: Both statements A and B use symbol operators correctly. && represents AND, and || represents OR, both of which are valid in SQL environments that allow symbol operators.

39
Q

What does the SQL operator != represent?
A) Equals
B) Not equals
C) Greater than
D) Less than

A

B) Not equals

Explanation: The != operator in SQL is used to compare two values, indicating “not equals.”

40
Q

How can you write a query to select all records where the username is not ‘john’ using symbol operators?
A) SELECT * FROM logins WHERE username != ‘john’;
B) SELECT * FROM logins WHERE username ! ‘john’;
C) SELECT * FROM logins WHERE !username = ‘john’;
D) SELECT * FROM logins WHERE username = ! ‘john’;

A

A) SELECT * FROM logins WHERE username != ‘john’;

Explanation: The correct use of the not equals symbol operator is !=, making option A the correct way to select records where the username is not ‘john’.