Mysql interview questions and answers


Questions : 1how to do login in mysql with unix shell
Answers :1By below method if password is pass and user name is root
# [mysql dir]/bin/mysql -h hostname -u root -p pass
  
Questions : 2how you will Create a database on the mysql server with unix shell
Answers : 2mysql> create database databasename;
  
Questions : 3how to list or view all databases from the mysql server.
Answers : 3mysql> show databases;
  
Questions : 4How Switch (select or use) to a database.
Answers : 4mysql> use databasename;
  
Questions : 5How To see all the tables from a database of mysql server.
Answers : 5mysql> show tables;
  
Questions : 6How to see table's field formats or description of table .
Answers : 6mysql> describe tablename;
  
Questions : 7How to delete a database from mysql server.
Answers : 7mysql> drop database databasename;
  
Questions : 8How we get Sum of column
Answers : 8mysql> SELECT SUM(*) FROM [table name];
  
Questions : 9How to delete a table
Answers : 9mysql> drop table tablename;
  
Questions : 10How you will Show all data from a table.
Answers : 10mysql> SELECT * FROM tablename;
  
Questions : 11How to returns the columns and column information pertaining to the designated table
Answers : 11mysql> show columns from tablename;
  
Questions : 12How to Show certain selected rows with the value "pcds"
Answers : 12mysql> SELECT * FROM tablename WHERE fieldname = "pcds";
  
Questions : 13How will Show all records containing the name "sonia" AND the phone number '9876543210'
Answers : 13mysql> SELECT * FROM tablename WHERE name = "sonia" AND phone_number = '9876543210';
  
Questions : 14How you will Show all records not containing the name "sonia" AND the phone number '9876543210' order by the phone_number field.
Answer : 14mysql> SELECT * FROM tablename WHERE name != "sonia" AND phone_number = '9876543210' order by phone_number;
  
Questions : 15How to Show all records starting with the letters 'sonia' AND the phone number '9876543210'
Answers : 15mysql> SELECT * FROM tablename WHERE name like "sonia%" AND phone_number = '9876543210';
  
Questions : 16How to show all records starting with the letters 'sonia' AND the phone number '9876543210' limit to records 1 through 5.
Answers : 16mysql> SELECT * FROM tablename WHERE name like "sonia%" AND phone_number = '9876543210' limit 1,5;
  
Questions : 16Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with r.
Answer : 16mysql> SELECT * FROM tablename WHERE rec RLIKE "^r";
  
Questions : 17How you will Show unique records.
Answer : 17mysql> SELECT DISTINCT columnname FROM tablename;
  
Questions : 18how we will Show selected records sorted in an ascending (asc) or descending (desc)
Answer : 18mysql> SELECT col1,col2 FROM tablename ORDER BY col2 DESC;

mysql> SELECT col1,col2 FROM tablename ORDER BY col2 ASC;
  
Questions : 19how to Return total number of rows.
Answers : 19mysql> SELECT COUNT(*) FROM tablename;
  
Questions : 20How to Join tables on common columns.
Answer : 20mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id
  
Questions : 21How to Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.
Answer : 21# mysql -u root -p

mysql> use mysql;

mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));

mysql> flush privileges;
  
Questions : 22How to Change a users password from unix shell.
Answers : 22# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
  
Questions : 23How to Change a users password from MySQL prompt. Login as root. Set the password. Update privs.
Answer : 23# mysql -u root -p

mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');

mysql> flush privileges;
  
Questions : 24How to Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.
Answer : 24# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
  
Questions : 25How to Set a root password if there is on root password.
Answer : 25# mysqladmin -u root password newpassword
  
Questions : 26How to Update a root password.
Answer : 26# mysqladmin -u root -p oldpassword newpassword
  
Questions : 27How to allow the user "sonia" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.
Answers : 27# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to sonia@localhost identified by 'passwd';
mysql> flush privileges;
  
Questions : 28How to give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.
Answers : 28# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;
or
mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;
  
Questions : 29How To update info already in a table and Delete a row(s) from a table.
Answer : 29mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
mysql> DELETE from [table name] where [field name] = 'whatever';
  
Questions : 30How to Update database permissions/privilages.
Answer : 30mysql> flush privileges;
  
Questions : 31How to Delete a column and Add a new column to database
Answer : 31mysql> alter table [table name] drop column [column name];
mysql> alter table [table name] add column [new column name] varchar (20);
  
Questions : 32Change column name and Make a unique column so we get no dupes.
Answer : 32mysql> alter table [table name] change [old column name] [new column name] varchar (50);
mysql> alter table [table name] add unique ([column name]);
  
Questions : 33How to make a column bigger and Delete unique from table.
Answer : 33mysql> alter table [table name] modify [column name] VARCHAR(3);
mysql> alter table [table name] drop index [colmn name];
  
Questions : 34How to Load a CSV file into a table
Answer : 34mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
  
Questions : 35How to dump all databases for backup. Backup file is sql commands to recreate all db's.
Answer : 35# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
  
Questions : 36How to dump one database for backup.
Answer : 36# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
  
Questions : 37How to dump a table from a database.
Answer : 37# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
  
Questions : 38Restore database (or database table) from backup.
Answer : 38# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
  
Questions : 39How to Create Table show Example
Answer : 39mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));
Questions : 40How to search second maximum(second highest) salary value(integer)from table employee (field salary)in the manner so that mysql gets less load?
Answers : 40
By below query we will get second maximum(second highest) salary value(integer)from table employee (field salary)in the manner so that mysql gets less load?
SELECT DISTINCT(salary) FROM employee order by salary desc limit 1 , 1 ; 
(This way we will able to find out 3rd highest , 4th highest salary so on just need to change limit condtion like LIMIT 2,1 for 3rd highest and LIMIT 3,1 for 4th
some one may finding this way useing below query that taken more time as compare to above query SELECT salary FROM employee where salary < (select max(salary) from employe) order by salary DESC limit 1 ;

Comments

Popular posts from this blog

SQL Tutorial with HSQLDB

Hibernate tutorial with HSQLDB

Java Interview Questions & Answers: user defined key class