Practical Question Answers (SQL, JavaScript, C Programming and PHP)

SQL Practical Questions/Answers

Create a new database named “company”.

Answer: CREATE DATABASE company;

Create a table named “employees” with columns for id (integer, primary key), name (varchar), age (integer), and department (varchar).

Answer: CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(255), age INT, department VARCHAR(255));

Create a table named “products” in the “company” database with columns for id (integer, primary key), name (varchar), price (decimal), and quantity (integer).

Answer: CREATE TABLE company.products (id INT PRIMARY KEY, name VARCHAR(255), price DECIMAL(10, 2), quantity INT);

Alter the “employees” table to add a new column named “salary” (integer).

Answer: ALTER TABLE employees ADD COLUMN salary INT;

Alter the “products” table to add a new column named “description” (text).

Answer: ALTER TABLE company.products ADD COLUMN description TEXT;

Alter the “products” table to modify the data type of the “price” column to FLOAT.

Answer: ALTER TABLE company.products MODIFY COLUMN price FLOAT;

Alter the “products” table to rename the column “quantity” to “stock”.

Answer: ALTER TABLE company.products RENAME COLUMN quantity TO stock;

Drop the “salary” column from the “employees” table.

Answer: ALTER TABLE employees DROP COLUMN salary;

Insert a new record into the “products” table in the “company” database with values for id, name, price, and stock.

Answer: INSERT INTO company.products (id, name, price, stock) VALUES (1, 'Laptop', 999.99, 50);

Insert multiple records into the “products” table in the “company” database with values for id, name, price, and stock.

Answer: INSERT INTO company.products (id, name, price, stock) VALUES (2, 'Smartphone', 499.99, 100), (3, 'Tablet', 299.99, 75), (4, 'Headphones', 99.99, 200);

Insert a new record into the “employees” table with values for id, name, age, and department.

Answer: INSERT INTO employees (id, name, age, department) VALUES (1, 'John', 35, 'HR');

Insert a new record into the “employees” table with values for id, name, age, department, and salary.

Answer: INSERT INTO employees (id, name, age, department, salary) VALUES (4, 'Alice', 28, 'Marketing', 50000);

Update the price of the product with id 2 to 549.99 in the “products” table.

Answer: UPDATE company.products SET price = 549.99 WHERE id = 2;

Update the stock of the product with id 1 to 40 and its price to 899.99 in the “products” table.

Answer: UPDATE company.products SET stock = 40, price = 899.99 WHERE id = 1;

Update the age of the employee with id 1 to 30 in the “employees” table.

Answer: UPDATE employees SET age = 30 WHERE id = 1;

Update the department of the employee with id 2 to ‘Finance’ in the “employees” table.

Answer: UPDATE employees SET department = 'Finance' WHERE id = 2;

Delete the record with id 3 from the “products” table in the “company” database.

Answer: DELETE FROM company.products WHERE id = 3;

Delete all records from the “products” table where the price is less than 100.

Answer: DELETE FROM company.products WHERE price < 100;

Delete all records from the “products” table.

Answer: DELETE FROM company.products;

Delete all records from the “employees” table where the age is greater than 50.

Answer: DELETE FROM employees WHERE age > 50;

Delete the employee with id 3 from the “employees” table.

Answer: DELETE FROM employees WHERE id = 3;

Select all columns from the “employees” table.

Answer: SELECT * FROM employees;

Select only the “name” and “age” columns from the “employees” table.

Answer: SELECT name, age FROM employees;

Select all employees from the “employees” table where the department is ‘IT’.

Answer: SELECT * FROM employees WHERE department = 'IT';

Select all employees from the “employees” table where the age is between 25 and 35.

Answer: SELECT * FROM employees WHERE age BETWEEN 25 AND 35;

Select all employees from the “employees” table sorted by age in ascending order.

Answer: SELECT * FROM employees ORDER BY age ASC;

Select the oldest employee from the “employees” table.

Answer: SELECT * FROM employees ORDER BY age DESC LIMIT 1;

Retrieve unique values from the “department” column in the “employees” table.

Answer: SELECT DISTINCT department FROM employees;

Retrieve the first 10 records from the “orders” table, ordered by the “order_date” column in descending order.

Answer: SELECT * FROM orders ORDER BY order_date DESC LIMIT 10;

Find the total number of records in the “products” table.

Answer: SELECT COUNT(*) FROM products;

Update the “price” column of the “products” table to 50 where the “category” is ‘Electronics’.

Answer: UPDATE products SET price = 50 WHERE category = 'Electronics';

Delete all records from the “employees” table where the “department” is ‘HR’.

Answer: DELETE FROM employees WHERE department = 'HR';

Retrieve the average salary of employees from the “employees” table.

Answer: SELECT AVG(salary) FROM employees;

Find the highest salary from the “employees” table.

Answer: SELECT MAX(salary) FROM employees;

Retrieve the names of employees who have joined after ‘2020-01-01’.

Answer: SELECT name FROM employees WHERE join_date > '2020-01-01';

Retrieve the second highest salary from the “employees” table.

Answer: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);

Calculate the total sales amount for each product from the “sales” table and group them by product ID.

Answer: SELECT product_id, SUM(amount) AS total_sales FROM sales GROUP BY product_id;

Retrieve all orders where the order amount is greater than $1000 from the “orders” table.

Answer: SELECT * FROM orders WHERE order_amount > 1000;

Find the number of orders placed by each customer from the “orders” table and group them by customer ID.

Answer: SELECT customer_id, COUNT(order_id) AS order_count FROM orders GROUP BY customer_id;

Find the top 5 best-selling products from the “products” table based on the quantity sold.

Answer: SELECT product_id, SUM(quantity_sold) AS total_quantity FROM products GROUP BY product_id ORDER BY total_quantity DESC LIMIT 5;

Retrieve all columns from the “employees” table where the salary is greater than $50000.

Answer: SELECT * FROM employees WHERE salary > 50000;

Find the total number of customers from the “customers” table.

Answer: SELECT COUNT(*) FROM customers;

Retrieve the names and ages of customers from the “customers” table who are older than 30 years.

Answer: SELECT name, age FROM customers WHERE age > 30;

Update the “status” column of the “orders” table to ‘Completed’ where the “order_date” is before ‘2023-01-01’.

Answer: UPDATE orders SET status = 'Completed' WHERE order_date < '2023-01-01';

Delete all records from the “products” table where the “category” is ‘Books’.

Answer: DELETE FROM products WHERE category = 'Books';

Retrieve the names and email addresses of customers from the “customers” table where the email address contains ‘@gmail.com’.

Answer: SELECT name, email FROM customers WHERE email LIKE '%@gmail.com%';

Find the average salary of employees in the “sales” department from the “employees” table.

Answer: SELECT AVG(salary) FROM employees WHERE department = 'Sales';

JavaScript Practical Questions

  1. Write a JavaScript function to add two numbers.
  2. Create a function to subtract two numbers.
  3. Write a function to multiply two numbers.
  4. Create a function to divide two numbers.
  5. Write a JavaScript function to find the square of a number.
  6. Create a function to find the cube of a number.
  7. Write a JavaScript function to find the maximum of two numbers.
  8. Create a function to find the minimum of two numbers.
  9. Write a function to check if a number is positive, negative, or zero.
  10. Create a JavaScript function to check if a number is even or odd.
  11. Write a function to check if a number is a multiple of another number.
  12. Create a JavaScript function to check if a year is a leap year.
  13. Write a function to calculate the area of a rectangle given its length and width.
  14. Create a function to calculate the perimeter of a rectangle given its length and width.
  15. Write a JavaScript function to calculate the area of a circle given its radius.
  16. Create a function to calculate the circumference of a circle given its radius.
  17. Write a function to convert Celsius to Fahrenheit.
  18. Create a JavaScript function to convert Fahrenheit to Celsius.
  19. Write a function to generate a random integer between two specified values.
  20. Create a function to generate a random number between 0 and 1.
  21. Write a JavaScript function to generate an array of specified lengths filled with random integers.
  22. Create a function to find the factorial of a given number.
  23. Write a JavaScript function to check if a given number is prime.
  24. Create a function to find the sum of all numbers in an array.
  25. Write a function to find the average of numbers in an array.
  26. Create a JavaScript function to find the largest element in an array.
  27. Write a function to find the smallest element in an array.
  28. Create a function to find the median of numbers in an array.
  29. Write a JavaScript function to reverse a string.
  30. Create a function to check if a given string is a palindrome.
  31. Write a function to count the number of vowels in a string.
  32. Create a JavaScript function to count the number of words in a sentence.
  33. Write a function to find the longest word in a sentence.
  34. Create a function to remove duplicates from an array.
  35. Write a JavaScript function to sort an array of numbers in ascending order.
  36. Create a function to shuffle elements in an array.
  37. Write a function to truncate a string to a specified length.
  38. Create a JavaScript function to capitalize the first letter of each word in a sentence.
  39. Write a function to find the difference between two dates in days.
  40. Create a function to check if two arrays are equal.
  41. Write a JavaScript function to find the intersection of two arrays.
  42. Create a function to merge two sorted arrays into one sorted array.
  43. Write a function to remove a specific element from an array.
  44. Create a JavaScript function to remove all false values from an array.
  45. Write a function to find the most frequent element in an array.
  46. Create a function to check if an array contains a specific value.
  47. Write a JavaScript function to flatten a nested array.
  48. Create a function to rotate elements in an array to the left by a given number of positions.
  49. Write a function to generate a Fibonacci sequence up to a specified number of terms.
  50. Create a JavaScript function to check if a string contains unique characters.\

C Programming Practical Questions

  1. Write a C program to find the sum of two numbers using a function.
  2. Create a program to calculate the factorial of a number using a function.
  3. Write a C program to check if a number is prime using a function.
  4. Create a program to find the maximum of two numbers using a function.
  5. Write a C program to reverse a string using a function.
  6. Create a program to swap two numbers using call by reference.
  7. Write a C program to sort an array of integers using functions.
  8. Create a program to find the length of a string using a function.
  9. Write a C program to concatenate two strings using functions.
  10. Create a program to find the square of a number using a function.
  11. Write a C program to implement a structure representing a student with fields for name, roll number, and marks in three subjects.
  12. Create a program to calculate the average marks of a student using a structure.
  13. Write a C program to display details of students stored in an array of structures.
  14. Create a program to find the student with the highest marks using structures.
  15. Write a C program to implement a union representing the details of an employee with fields for employee ID, name, and salary.
  16. Create a program to display details of employees stored in an array of unions.
  17. Write a C program to find the employee with the highest salary using unions.
  18. Create a program to swap two numbers using pointers.
  19. Write a C program to reverse an array using pointers.
  20. Create a program to find the sum of elements in an array using pointers.
  21. Write a C program to find the length of a string using pointers.
  22. Create a program to concatenate two strings using pointers.
  23. Write a C program to implement a linked list to store integers.
  24. Create a program to insert a node at the beginning of a linked list.
  25. Write a C program to insert a node at the end of a linked list.
  26. Create a program to delete a node from a linked list.
  27. Write a C program to search for a node in a linked list.
  28. Create a program to reverse a linked list.
  29. Write a C program to implement a stack using an array.
  30. Create a program to push an element into a stack.
  31. Write a C program to pop an element from a stack.
  32. Create a program to implement a queue using an array.
  33. Write a C program to enqueue an element into a queue.
  34. Create a program to dequeue an element from a queue.
  35. Write a C program to implement a stack using linked list.
  36. Create a program to push an element into a stack implemented using linked list.
  37. Write a C program to pop an element from a stack implemented using linked list.
  38. Create a program to implement a queue using linked list.
  39. Write a C program to enqueue an element into a queue implemented using linked list.
  40. Create a program to dequeue an element from a queue implemented using linked list.
  41. Write a C program to copy contents of one file to another.
  42. Create a program to count the number of characters, words, and lines in a file.
  43. Write a C program to find the largest word in a file.
  44. Create a program to append text to a file.
  45. Write a C program to merge two files into a third file.
  46. Create a program to sort lines of text in a file alphabetically.
  47. Write a C program to encrypt and decrypt text in a file.
  48. Create a program to find and replace a word in a file.
  49. Write a C program to search for a specific line in a file.
  50. Create a program to display the contents of a file in reverse order.

PHP Practical Questions

  1. Write a PHP script that connects to a MySQL database and inserts user input from a form into a database table.
  2. Create a PHP script that retrieves user input from a form using the POST method, performs a calculation, and displays the result.
  3. Write a PHP script that retrieves user input from a form using the GET method, performs a database query based on the input, and displays the results.
  4. Create a PHP script that connects to a MySQL database, retrieves user input from a form, updates a specific record in the database based on the input, and displays a success message.
  5. Write a PHP script that connects to a MySQL database, retrieves user input from a form using both POST and GET methods, performs a calculation based on the input, and displays the result.
  6. Create a PHP script that retrieves user input from a form, validates the input, performs a database query to check if the input exists in the database, and displays appropriate messages.
  7. Write a PHP script that connects to a MySQL database, retrieves user input from a form using the POST method, inserts the input into multiple database tables, and displays a success message.
  8. Create a PHP script that retrieves user input from a form using the GET method, performs a database query to fetch relevant data based on the input, and displays the data in a formatted manner.
  9. Write a PHP script that connects to a MySQL database, retrieves user input from a form using the POST method, performs a calculation based on the input, and updates the result in the database.
  10. Create a PHP script that retrieves user input from a form using the GET method, performs a database query to fetch relevant data based on the input, calculates the total based on the retrieved data, and displays the total to the user.