Sample interview problems

Printing employee names and salaries from a table

This problem deals with using a cursor to loop through a table of employee data in order to print the employee's name and salary.

Problem

Given a table of employee data, write a cursor to loop through the table and print the employee's name and salary.
Example input:
Name | Salary
John | 50000
Mary | 60000
Example output:
John's salary is 50000
Mary's salary is 60000

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by AskAI
DECLARE @Name VARCHAR(50)
DECLARE @Salary INT

DECLARE Cursor1 CURSOR FOR
SELECT Name, Salary
FROM Employee

OPEN Cursor1

FETCH NEXT FROM Cursor1 INTO @Name, @Salary

WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT @Name + s salary is ' + CAST(@Salary AS VARCHAR(10))
    FETCH NEXT FROM Cursor1 INTO @Name, @Salary
END

CLOSE Cursor1
DEALLOCATE Cursor1

/*

The solution above is optimal because it uses a cursor to loop through the table and print the employee's name and salary.

*/

A.I. Evaluation of the Solution

The candidate's solution is complete and solves the problem. The candidate has used a cursor to loop through the table and print the employee's name and salary. This is the optimal solution.

Evaluated at: 2022-10-20 19:38:42

Community solutions:

Check these solutions from our community and artificial intelligence:
The candidate's solution is complete and solves the problem. The candidate has used a cursor to loop through the table and print the employee's name and salary. This is the optimal solution.